“bounds”和“frame”的区别在哪里?实际上,“bounds”为什么会存在?“bounds”的大小等于“frame”的大小,“bounds”的原点应始终为0,0。
“bounds”和“frame”的区别在哪里?实际上,“bounds”为什么会存在?“bounds”的大小等于“frame”的大小,“bounds”的原点应始终为0,0。
根据iOS的视图和窗口架构编程指南:
一个视图对象使用其frame、bounds和center属性跟踪其大小和位置:
frame属性包含框架矩形,它指定了视图在其父视图坐标系统中的大小和位置。
bounds属性包含边界矩形,它指定了视图(以及其内容原点)在视图自己的本地坐标系统中的大小。
center属性包含已知视图在父视图坐标系统中心点的中心点。
这里是对上述解释的很好的可视化:
NSView
),相关文档应该是《Cocoa 视图编程指南》,而不是你链接的 iOS 版本。请注意,在 Mac 上默认情况下,坐标系是翻转的,原点位于左下角。 - Rob KenigerIf your view controller has the top-level non-window view (i.e., it’s the bottom-most view controller), then
self.frame
is always in portrait orientation. Wha? Yes, always in portrait – what changes is the transform of your view. So your
self.bounds
is always accurate (keeping in mind the last point), but
self.frame
may or may not give the aspect ratio that the user is really seeing, since each view’s frame is reported in terms of the superview’s coordinates, and takes into account any transforms applied to the view.
frame 是在父视图坐标系中的坐标值
bounds 用于绘制视图内容,当绘制完成后,系统会使用变换操作将内容分配到视图的 frame 中