对 loadView 中的视图初始化感到沮丧

发布于 2024-12-29 22:46:15 字数 360 浏览 2 评论 0原文

在为 ios 进行开发时,我以编程方式完成所有操作,因此我在 loadView 中为所有视图控制器手动初始化视图控制器视图(这就是 Apple 所说的做法)。这样做让我感到沮丧的是,视图控制器框架最初是 {{0, 20}, {320, 460}} 没有正确解释我的应用程序中的任何导航栏或标签栏。视图最终在 viewDidAppear 中设置了正确的框架,但到那时做任何事情都为时已晚。有没有办法让它更早地识别视图布局而无需手动计算?

这很令人沮丧,因为我总是必须手动检查手机是否处于横向或纵向模式以考虑旋转,并且如果我必须以无法自动调整大小的特定方式设置子视图,那么我也必须进行手动计算。

苹果是否推荐这样做的任何模式,或者是否有办法让它更早地识别视图布局而不必手动计算?

I do everything programmatically when developing for the ios, so I manually initialize the view controllers view in loadView for all of my view controllers (this is what Apple says to do). What frustrates me about doing it this way is the viewcontrollers frame initially is {{0, 20}, {320, 460}} not correctly accounting for any navbars or tabbars I have in my app. The view finally sets its proper frame in viewDidAppear, but by that time it's too late to do anything. Is there anyway to make it recognize the view layout earlier without having calculate it manually?

It's frustrating because I always have to manually check to see if the phone is in landscape or portrait mode to account for rotation, and if I have to set up my subviews in a specific way that I can't do with autoresizing, then I also have to do manual calculations.

Is there any pattern that apple recommends for doing this, or is there anyway to get it to recognize the view layout earlier without having to calculate it manually?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你怎么这么可爱啊 2025-01-05 22:46:15

如果您使用的是 iOS 5,则应该在 UIViewController 子类的 viewDidLayoutSubviewsviewWillLayoutSubviews 方法中传递正确的帧。这是您应该计算布局的地方,它们在设备旋转时被调用。

否则,viewWillAppear 从 iOS 2.0 开始就存在,并且我的测试应用程序显示它通过了正确的框架。它不能解决运行中的轮换问题,但想必您可以在其他地方插入它。您不应该在游戏后期设置视图,但除了使用 viewDidLoad 中的 layoutIfNeeded (我没有设法在其中工作)过去)我不知道你在 iOS5 之前应该做什么。

If you are using iOS 5, you should get passed the correct frame in the viewDidLayoutSubviews and viewWillLayoutSubviews methods of your UIViewController subclass. This is where you should calculate your layout, and they are called when the device rotates.

Otherwise, viewWillAppear exists from iOS 2.0, and my test app shows that it is passed the proper frame. It doesn't solve the rotation mid-run, but presumably you can just hook into that elsewhere. You aren't meant to set up your views that late in the game, but other than playing around with layoutIfNeeded from viewDidLoad (that I didn't manage to get working in the past) I don't know what you are supposed to do pre iOS5.

橪书 2025-01-05 22:46:15

您可以确保您的视图具有正确的自动调整大小蒙版,并且一旦新尺寸出现,它应该全部调整。

[view setAutoresizingMask:UIViewAutoresizingFlexibleHeight];

这就是我通常所做的。否则,我在稍后的回调中调整布局 - 它是通过 viewWillAppear 调整导航栏的,不确定 viewDidLoad 是否太早。

You can make sure your view has the right autoresizing mask, and it should all adjust once the new size comes in.

[view setAutoresizingMask:UIViewAutoresizingFlexibleHeight];

That's what I usually do. Otherwise, I adjust the layout in a later callback - it's adjusted for the nav bar by viewWillAppear, not sure if viewDidLoad is too early.

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