gpt4 book ai didi

ios - UITabBarController 选项卡的旋转和 willRotateTo InterfaceOrientation 传播

转载 作者:可可西里 更新时间:2023-11-01 05:42:11 26 4
gpt4 key购买 nike

我的应用有四个标签:ABCD。它们的 UIViewControllerUITabBarController 管理。该应用程序支持旋转,因此每个 View Controller 都向 shouldAutorotateToInterfaceOrientation 返回 YES

使用 springs 和 struts,大部分旋转由 iOS 自动完成。然而,选项卡A 还需要进一步定位,这是在其VC 的willRotateToInterfaceOrientation 方法中完成的。

选择选项卡的VC a 并旋转屏幕时,VC接收 willrotateToInterfaceorientation message(由ios从 uit> uitabbarcontroller 传播), 结果旋转是正确的。

However, when the selected tab is B and the screen is rotated, A's willRotateToInterfaceOrientation is not called.说得通。但是,如果我随后选择选项卡 A,我只会得到应用其 Spring 和支柱的结果,而不会得到其 willRotateToInterfaceOrientation 完成的后处理。

纠结了一段时间,网上也找不到解决办法,我想出了以下办法。我将 UITabBarController 子类化,并在其 willRotateToInterfaceOrientation 中调用所有 VC 的 willRotateToInterfaceOrientation,无论 selectedViewController 是哪一个:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (self.viewControllers != nil) {
for (UIViewController *v in self.viewControllers)
[v willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
}

它有效,但它看起来像一个 hack,我的问题是我是否在做正确的事情。有没有办法告诉 iOS 在屏幕旋转后第一次显示之前总是调用 VC 的 willRotateToInterfaceOrientation

最佳答案

处理自定义布局的最佳方法是子类化 UIView 并覆盖 layoutSubviews 方法。每当 View 的大小发生变化时(以及其他时间),系统都会将 layoutSubviews 发送到 View 。因此,当您的 View A 即将以不同的尺寸出现在屏幕上时(因为界面在 View B 在屏幕上时旋转了),系统会向 View A 发送 layoutSubviews 消息,即使它没有t 向 View Controller A 发送 willRotateToInterfaceOrientation: 消息。

如果您的目标是 iOS 5.0 或更高版本,您可以覆盖 UIViewController 子类的 viewDidLayoutSubviews 方法并在那里进行布局,而不是继承 UIView。我更喜欢在我的 View 的 layoutSubviews 中执行此操作,以将我的 View 特定逻辑与我的控制逻辑分开。

willRotateToInterfaceOrientation: 中进行布局也是一个坏主意,因为系统会在实际更改 View 大小之前以及旋转动画 block 之前发送该消息。它在旋转动画 block 内发送 willAnimateRotationToInterfaceOrientation:duration:layoutSubviewsviewDidLayoutSubviews 消息,因此 subview 的重新定位将是动画的,如果旋转期间 View 在屏幕上。

关于ios - UITabBarController 选项卡的旋转和 willRotateTo InterfaceOrientation 传播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11090931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com