[英]Allow One View to Support Multiple Orientations While Others Do Not iPhone
我遇到了通过UITabBarController
控制多个视图的情况。 这些视图之一是通过UINavigationController
控制的。 该应用程序应仅对所有视图都支持纵向...除了一个单独的视图之外。 该视图被推到导航控制器的堆栈上,并且应该允许纵向和横向。
我已经尝试过所有可以想到的解决方案,但是,要么解决方案不起作用,要么更糟糕的是完全无法预测。
有人以干净的方式解决了这个问题吗?
使用presentModalViewController
或pushViewController
呈现的视图控制器应使用以下方法支持任何方向:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
使用这种方法,当您以横向显示控制器时,它也将正确以横向显示。
我有同样的问题。 您必须为UITabBarController
实现shouldAutorotateToInterfaceOrientation:
UITabBarController
并为要在所有视图控制器中支持的所有方向返回YES。 并且对于所有其他视图控制器,使用相同的方法仅对要在每个视图控制器中支持的方向返回YES。
要实现shouldAutorotateToInterfaceOrientation:
对于UITabBarController,可以将UITabBarController子类化,但是一种更简单的方法是在UITabBarController上实现类别,然后仅实现该方法:
@implementation UITabBarController(orientation)
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation
{
return YES; // or whatever you need
}
@end
注意:输入网络浏览器; 没有编译。 :-)
您可以在最后将它放在您的应用程序委托.m文件中。
我发现UITabBarController仅对肖像返回YES,这显然也会影响所有从属视图控制器。 以我为例,我有一个下级视图控制器,我想同时支持纵向和横向,这解决了这一问题。
如果导航栏位于选项卡栏的内部,则唯一的选择是将特定视图显示为模式视图(选项卡栏要求所有视图都支持自动旋转的方向)。 所以基本上需要景观的单视图控制器应该实现
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
// Create the landscape version of the view and present it modally
}
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
然后,通过检测肖像方向并将其用于父级,以模态视图控制器的相同方法关闭模态视图[self.parentViewController dismissModalViewControllerAnimated: animated]
对于iOS-6,我已经做到了,它运行得很好
(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}
(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
这将为您提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.