UIView和它的子类都有frame和bounds属性。有什么不同?
当前回答
以上所有答案都是正确的,以下是我的看法:
为了区分框架和边界概念,开发者应该阅读:
相对于父视图(父视图),它包含在= FRAME中 相对于它自己的坐标系,决定了它的子视图location = BOUNDS
"bounds"是令人困惑的,因为它给人的印象是坐标是它所设置的视图的位置。但这些是有关系的,可以根据坐标系常数进行调整。
其他回答
以上所有答案都是正确的,以下是我的看法:
为了区分框架和边界概念,开发者应该阅读:
相对于父视图(父视图),它包含在= FRAME中 相对于它自己的坐标系,决定了它的子视图location = BOUNDS
"bounds"是令人困惑的,因为它给人的印象是坐标是它所设置的视图的位置。但这些是有关系的,可以根据坐标系常数进行调整。
让我加上我的5美分。
视图的父视图使用Frame将其放置在父视图中。
视图本身使用Bounds来放置它自己的内容(就像滚动视图在滚动时所做的那样)。请参见clipsToBounds。边界也可以用来放大/缩小视图的内容。
类比: 框架~电视屏幕 边界~相机(缩放,移动,旋转)
这里有一个很棒的视频(来自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像素、点数、单位]
框架与边界
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.
推荐文章
- 如何从UIImage (Cocoa Touch)或CGImage (Core Graphics)获取像素数据?
- 在Swift中如何调用GCD主线程上的参数方法?
- iOS -构建失败,CocoaPods无法找到头文件
- 为什么单元测试中的代码不能找到包资源?
- Xcode构建失败“架构x86_64未定义的符号”
- 新的自动引用计数机制是如何工作的?
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 从Cocoa应用程序执行一个终端命令
- 如何使用UIVisualEffectView来模糊图像?
- 自动调整掩码大小编程vs接口生成器/ xib / nib
- 我如何输入RGB值到接口生成器?
- 裁剪一个UIImage
- 在这个块中强烈地捕获self很可能导致保留循环
- iPhone上UIView和UILabels的渐变
- 自动布局- UIButton的固有大小不包括标题插入