呈现视图控制器会触发 UIApplicationDidChangeStatusBarOrientationNotification
Presenting a view controller triggers UIApplicationDidChangeStatusBarOrientationNotification
我有一个通用应用程序,它同时支持 iPad 和 iPhone,并且在 iPhone 上它支持 UIInterfaceOrientationPortraitUpsideDown
。
当我 运行 应用程序在 iPhone 上时,然后将设备旋转到 UIInterfaceOrientationPortraitUpsideDown
,然后模态显示一个视图控制器,如下所示:
UIViewController* vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController* navigationController = [[UINavigationController alloc]
initWithRootViewController:vc];
navigationController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:navigationController animated:YES completion:nil];
非常意外,这触发了UIApplicationDidChangeStatusBarOrientationNotification
。
好的,这可能是因为模态视图控制器不支持上下颠倒的纵向方向。让我们创建一个 UIViewController
的自定义子类来覆盖 supportedInterfaceOrientations
:
- (NSUInteger) supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
但没有区别,显示自定义 VC 仍然会触发通知。我还注意到通知 不是 在 iPad 上触发,仅在 iPhone 上触发。最后,行为在 iOS 8 和 iOS 7 中是相同的(具体来说,我正在测试 iOS 8.1 和 iOS 7.1,但我怀疑这有什么区别) .
问题:出现模态VC时发送此通知是否正常?为什么它只发生在 UIInterfaceOrientationPortraitUpsideDown
,而不发生在常规纵向或两个横向方向中的任何一个?为什么它只发生在 iPhone 而不是 iPad?
注意:我可以根据要求提供更完整、最小的 运行ning 示例。
问题确实是模态呈现的视图控制器不支持 UIInterfaceOrientationPortraitUpsideDown
。我添加 supportedInterfaceOrientations
覆盖的想法是正确的,但我出错的地方是我将覆盖添加到 UIViewController
的自定义子 class - 但这是不是 呈现的视图控制器!
正如我的代码片段所示,呈现的视图控制器是 UINavigationController
实例。因此,我的问题的一个正确解决方案是创建 UINavigationController
的 subclass 并将 supportedInterfaceOrientations
添加到该 subclass.
另一个正确的,但在我的例子中比 subclassing 更优雅的解决方案是使用 UINavigationControllerDelegate
协议。
第 1 步:修改我的问题中的代码片段:
UIViewController* vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController* navigationController = [[UINavigationController alloc]
initWithRootViewController:vc];
navigationController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
// --- THE NEXT LINE IS NEW ---
navigationController.delegate = self;
[self presentViewController:navigationController animated:YES completion:nil];
第2步:确保包含呈现代码(=上述代码段)的class采用UINavigationControllerDelegate
协议。
第 3 步(是的, 是 第 3 步):将此协议方法添加到新指定的委托 class:
- (NSUInteger) navigationControllerSupportedInterfaceOrientations:(UINavigationController*)navigationController
{
return UIInterfaceOrientationMaskAll;
}
如果您在很多地方展示视图控制器,您可能希望拥有一些采用 UINavigationControllerDelegate
的共享对象,但这是您的应用程序设计的一部分。
我有一个通用应用程序,它同时支持 iPad 和 iPhone,并且在 iPhone 上它支持 UIInterfaceOrientationPortraitUpsideDown
。
当我 运行 应用程序在 iPhone 上时,然后将设备旋转到 UIInterfaceOrientationPortraitUpsideDown
,然后模态显示一个视图控制器,如下所示:
UIViewController* vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController* navigationController = [[UINavigationController alloc]
initWithRootViewController:vc];
navigationController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:navigationController animated:YES completion:nil];
非常意外,这触发了UIApplicationDidChangeStatusBarOrientationNotification
。
好的,这可能是因为模态视图控制器不支持上下颠倒的纵向方向。让我们创建一个 UIViewController
的自定义子类来覆盖 supportedInterfaceOrientations
:
- (NSUInteger) supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
但没有区别,显示自定义 VC 仍然会触发通知。我还注意到通知 不是 在 iPad 上触发,仅在 iPhone 上触发。最后,行为在 iOS 8 和 iOS 7 中是相同的(具体来说,我正在测试 iOS 8.1 和 iOS 7.1,但我怀疑这有什么区别) .
问题:出现模态VC时发送此通知是否正常?为什么它只发生在 UIInterfaceOrientationPortraitUpsideDown
,而不发生在常规纵向或两个横向方向中的任何一个?为什么它只发生在 iPhone 而不是 iPad?
注意:我可以根据要求提供更完整、最小的 运行ning 示例。
问题确实是模态呈现的视图控制器不支持 UIInterfaceOrientationPortraitUpsideDown
。我添加 supportedInterfaceOrientations
覆盖的想法是正确的,但我出错的地方是我将覆盖添加到 UIViewController
的自定义子 class - 但这是不是 呈现的视图控制器!
正如我的代码片段所示,呈现的视图控制器是 UINavigationController
实例。因此,我的问题的一个正确解决方案是创建 UINavigationController
的 subclass 并将 supportedInterfaceOrientations
添加到该 subclass.
另一个正确的,但在我的例子中比 subclassing 更优雅的解决方案是使用 UINavigationControllerDelegate
协议。
第 1 步:修改我的问题中的代码片段:
UIViewController* vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController* navigationController = [[UINavigationController alloc]
initWithRootViewController:vc];
navigationController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
// --- THE NEXT LINE IS NEW ---
navigationController.delegate = self;
[self presentViewController:navigationController animated:YES completion:nil];
第2步:确保包含呈现代码(=上述代码段)的class采用UINavigationControllerDelegate
协议。
第 3 步(是的, 是 第 3 步):将此协议方法添加到新指定的委托 class:
- (NSUInteger) navigationControllerSupportedInterfaceOrientations:(UINavigationController*)navigationController
{
return UIInterfaceOrientationMaskAll;
}
如果您在很多地方展示视图控制器,您可能希望拥有一些采用 UINavigationControllerDelegate
的共享对象,但这是您的应用程序设计的一部分。