使用 Autolayout & 报告错误的帧尺码等级 [英] Incorrect frame being reported using Autolayout & Size Classes

查看:27
本文介绍了使用 Autolayout & 报告错误的帧尺码等级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的 UIViewController 中布置了一个 UIView.限制了它的高度和宽度,并使用约束将其垂直和水平居中,但是当我记录它的框架时,它报告的位置似乎适合完整的 sizeClass,但不是我正在查看的屏幕大小.

I've laid out a UIView in my UIViewController. Constrained its height and width, and centered it vertically and horizontally with constraints, but when I log its frame, it reports a location that seems appropriate to the full sizeClass, but not the size of the screen I'm looking at.

例如,默认尺寸类 UIViewController 的宽度为 600 像素,在该尺寸下,我的 UIView 距离左边缘 150 像素.当我在 iphone5 模拟器中运行时检查与左边缘的距离时,它仍然报告 150px.

For example the default size class UIViewController has a width of 600px, and in that size, my UIView is 150px from the left edge. When I check this distance from the left edge while running in the iphone5 simulator, it still reports 150px.

所有这一切的奇怪之处在于,UIView 本身仍然出现在预期(根据屏幕大小调整)的位置.

The weird thing about all of this, is that the UIView itself still appears in the expected (adjusted for screen-size) location.

推荐答案

对于后代:我遇到的问题 -- 在打印视图框架时获取不正确的值 -- 是由在 期间访问该框架引起的code>ViewDidLoad.此时在 ViewController 生命周期中,所有视图可能尚未布局.

For posterity: the problem I was having -- getting incorrect values when printing the frame of a view -- was caused by accessing that frame during ViewDidLoad. At this point in the ViewController lifecycle all views may have not been laid out.

因此,只有在 ViewDidLayoutSubviews 被调用后才能访问 UIView 属性/出口是最安全的.

Therefore, it is safest to access a UIView property/outlet only after ViewDidLayoutSubviews has been called.

我的意思是,它就在方法的名称中:您的视图已经布局......现在用它们做一些事情."

I mean, it's right there in the name of the method: "Your views have been laid out... now do stuff with them."

这篇关于使用 Autolayout & 报告错误的帧尺码等级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆