我有一个从 Nib 加载的 View Controller ,其中定义了高度为 346 像素的 ScrollView 。但是当我这样做时:
int h = scrollView.frame.size.height;
NSLog(@"%d",h);
在 viewDidLoad 中我得到 375。
我需要 viewDidLoad (或其他类似方法)中的正确高度,以便我可以根据高度以编程方式设置 ScrollView 的内容(不希望它垂直“溢出”,仅水平“溢出”,其分页) .
ScrollView 打开了每条自动调整大小线。 (全部为鲜红色)。奇怪的是,当 View 加载时,我可以看到 ScrollView 的高度(正确的)346 像素而不是 375,因此必须将其更改为 viewDidLoad 和显示 View 之间的正确值。
我在这里陷入了死胡同,任何获得正确大小的 View 的方法都会非常有帮助。预先感谢,如果您需要更多信息,请发表评论。
编辑:我解决这个问题的方法是将一个 0.1 秒的计时器放入 viewDidLoad 中,当调用该计时器时,我完成了我需要做的工作。不过,这有点像黑客,所以任何其他建议将不胜感激。
最佳答案
执行额外布局类型计算的正确位置通常是在 viewWillLayoutSubviews
或 viewDidLayoutSubviews
中。 Docs here 。这也来自“处理 View 旋转”部分:
The
viewWillLayoutSubviews
method is also called after the view is resized and positioned by its parent. If a view controller is not visible when an orientation change occurs, then the rotation methods are never called. However, theviewWillLayoutSubviews
method is called when the view becomes visible.
关于iphone - iOS:viewDidLoad 中的 View 高度设置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4839332/