UIView和它的子类都有frame和bounds属性。有什么不同?
当前回答
Frame相对于SuperView,而Bounds相对于NSView。
例子:40 X = Y = 60。也包含3个视图。这张图向你展示了清楚的想法。
其他回答
这里有一个很棒的视频(来自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像素、点数、单位]
试着运行下面的代码
- (void)viewDidLoad {
[super viewDidLoad];
UIWindow *w = [[UIApplication sharedApplication] keyWindow];
UIView *v = [w.subviews objectAtIndex:0];
NSLog(@"%@", NSStringFromCGRect(v.frame));
NSLog(@"%@", NSStringFromCGRect(v.bounds));
}
这段代码的输出是:
机箱设备方向为纵向
{{0, 0}, {768, 1024}}
{{0, 0}, {768, 1024}}
机箱设备方向为横向
{{0, 0}, {768, 1024}}
{{0, 0}, {1024, 768}}
显然,你可以看到框架和边界之间的区别
框架与边界
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.
框架是一个矩形,它定义了UIView相对于它的父视图。
bounds rect是定义NSView坐标系的值的范围。
也就是说,这个矩形中的任何东西都会显示在UIView中。
推荐文章
- 如何从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的固有大小不包括标题插入