iOS:viewDidLoad 中的视图高度设置不正确

发布于 2024-10-14 19:41:07 字数 531 浏览 1 评论 0原文

我有一个从笔尖加载的视图控制器,其中定义了高度为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千柳 2024-10-21 19:41:07

执行额外布局类型计算的正确位置通常是在 viewWillLayoutSubviewsviewDidLayoutSubviews 中。

viewWillLayoutSubviews 方法也会在视图被调用后调用
由其父级调整大小和位置。如果视图控制器不是
当方向发生变化时可见,然后旋转方法
从来没有被调用过。但是,调用了 viewWillLayoutSubviews 方法
当视图变得可见时。

The correct place to perform additional layout-type calculations is generally in viewWillLayoutSubviews or viewDidLayoutSubviews. Docs here. Also this from the section on "Handling View Rotations":

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, the viewWillLayoutSubviews method is called
when the view becomes visible.

許願樹丅啲祈禱 2024-10-21 19:41:07

看看这个链接
您的子视图尚未在超级视图中放置和布局。例如,在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文