UIView和它的子类都有frame和bounds属性。有什么不同?
当前回答
框架与边界
If you create a view at X:0, Y:0, width:400, height:400, its frame and bounds are the same. If you move that view to X:400, its frame will reflect that change but its bounds will not. Remember, the bounds is relative to the view’s own space, and internally to the view nothing has changed. If you transform the view, e.g. rotating it or scaling it up, the frame will change to reflect that, but the bounds still won’t – as far as the view is concerned internally, it hasn’t changed. If you change the bounds then it will change the content inside the frame because the origin of the bounds rectangle starts at a different part of the view.
其他回答
这里有一个很棒的视频(来自Sean Allen):
Swift - Bounds vs. Frame - iOS面试问题
框架是相对于父视图坐标的位置:
边界是相对于自身坐标系的位置。
和…如果视图是旋转的,它会变得更加棘手。(看视频!)
iOS框架与边界
frame and bounds有CGRect类型,这些属性你可以在UIView和CALayer中找到[关于]
Frame与边界、位置、变换有直接关系:
(x, y)依赖于parenView和 (宽度,高度)被边界占据的完整矩形。
范围:
(x, y) = 0,0 (width, height)是视图的宽度和高度
再举一个例子来说明框架和边界之间的区别。 在这个例子中:
视图B是视图a的子视图 视图B被移动到x:72, y: 22 视图B旋转了45度
let viewB = UIView(frame: CGRect(origin: CGPoint(x: 72, y: 22), size: CGSize(width: 20, height: 40)))
viewB.backgroundColor = .cyan
let radian = 45 * CGFloat.pi / 180
viewB.transform = CGAffineTransform(rotationAngle: radian)
viewA.addSubview(viewB)
[iOS像素、点数、单位]
框架是一个矩形,它定义了UIView相对于它的父视图。
bounds rect是定义NSView坐标系的值的范围。
也就是说,这个矩形中的任何东西都会显示在UIView中。
Frame相对于SuperView,而Bounds相对于NSView。
例子:40 X = Y = 60。也包含3个视图。这张图向你展示了清楚的想法。
框架与边界
If you create a view at X:0, Y:0, width:400, height:400, its frame and bounds are the same. If you move that view to X:400, its frame will reflect that change but its bounds will not. Remember, the bounds is relative to the view’s own space, and internally to the view nothing has changed. If you transform the view, e.g. rotating it or scaling it up, the frame will change to reflect that, but the bounds still won’t – as far as the view is concerned internally, it hasn’t changed. If you change the bounds then it will change the content inside the frame because the origin of the bounds rectangle starts at a different part of the view.
推荐文章
- 如何更改导航栏上“后退”按钮的标题
- 有没有办法从UITableView中移除分隔线?
- 如何在Objective-C中声明类级属性?
- 改变UISegmentedControl的字体大小
- 我可以强制UITableView隐藏分隔符之间的空单元格吗?
- 移动文本字段时,键盘出现迅速
- 让iPhone震动
- 以编程方式获取Bundle Identifier
- 在整个UIWindow中获取UIView的位置
- 保存字符串到NSUserDefaults?
- 如何从iPhone应用程序发送邮件
- UILabel对齐文本到中心
- 如何使用接口生成器创建的nib文件加载UIView
- 模拟器慢动作动画现在打开了吗?
- 滑动删除和“更多”按钮(就像iOS 7的邮件应用程序)