iOS:viewDidLoad 中的视图高度设置不正确
我有一个从笔尖加载的视图控制器,其中定义了高度为 346 像素的滚动视图。但是,当我这样做时:
int h = scrollView.frame.size.height;
NSLog(@"%d",h);
在 viewDidLoad 中,我得到 375。
我需要 viewDidLoad (或其他类似方法)中的正确高度,以便我可以根据高度以编程方式设置滚动视图的内容(不希望它“溢出”)垂直,仅水平,其分页)。
滚动视图打开了每一条自动调整大小线。 (全部为鲜红色)。奇怪的是,当视图加载时,我可以看到滚动视图的高度(正确的)是 346 像素而不是 375,因此必须将其更改为 viewDidLoad 和显示视图之间的正确值。
我在这里陷入了死胡同,任何获得适当大小的视图的方法都会非常有帮助。预先感谢,如果您需要更多信息,请发表评论。
编辑:我解决这个问题的方法是将一个 0.1 秒的计时器放入 viewDidLoad 中,当调用该计时器时,我完成了我需要做的工作。不过,这有点黑客行为,所以任何其他建议将不胜感激。
I have a view controller loaded from a nib where I have a scroll view defined with a height of 346 pixels. However when I do:
int h = scrollView.frame.size.height;
NSLog(@"%d",h);
in viewDidLoad I get 375.
I need the proper height in viewDidLoad (or another similar method) so that I can programatically set the contents of the scroll view depending on the height (don't want it to 'overflow' vertically, only horizontally, its paged).
The scroll view has every single autoresizing line turned on. (all bright red). Strangely, when the view loads I can see that the scroll view is (the proper) 346 pixels high not 375, so it must get changed to the proper value somewhere between viewDidLoad and displaying the view.
I'm at a dead end here and any way to get the proper size of a view would be immensely helpful. Thanks in advance, and if you need any more info please just comment.
edit: The way I solved it was by putting a timer for 0.1 seconds into viewDidLoad, and when that timer is called I did the work I needed to do. This is a bit of a hack though so any other advice would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
执行额外布局类型计算的正确位置通常是在
viewWillLayoutSubviews
或viewDidLayoutSubviews
中。The correct place to perform additional layout-type calculations is generally in
viewWillLayoutSubviews
orviewDidLayoutSubviews
. Docs here. Also this from the section on "Handling View Rotations":看看这个链接。
您的子视图尚未在超级视图中放置和布局。例如,在
viewDidAppear:
中获取视图的大小。Take a look at this link.
You subview hasn't been placed and layouted within the superview. Get your view's size in
viewDidAppear:
for example.