简体   繁体   English

允许一个视图支持多种方向,而其他视图不支持iPhone

[英]Allow One View to Support Multiple Orientations While Others Do Not iPhone

I have a situation where I have multiple views controlled via UITabBarController . 我遇到了通过UITabBarController控制多个视图的情况。 One of those views is controlled via a UINavigationController . 这些视图之一是通过UINavigationController控制的。 The application should only support Portrait for all views... except one single solitary view. 该应用程序应仅对所有视图都支持纵向...除了一个单独的视图之外。 This view is pushed onto the stack of the navigation controller and should allow both portrait AND landscape. 该视图被推到导航控制器的堆栈上,并且应该允许纵向和横向。

I've tried every solution that I can think of but, either the solution just doesn't work or worse is completely unpredictable. 我已经尝试过所有可以想到的解决方案,但是,要么解决方案不起作用,要么更糟糕的是完全无法预测。

Has anybody solved this issue in a clean way? 有人以干净的方式解决了这个问题吗?

The view's controller that you present either using presentModalViewController OR pushViewController should support any orientation, using this method: 使用presentModalViewControllerpushViewController呈现的视图控制器应使用以下方法支持任何方向:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

With this method, when you present your controller in landscape orientation, it will correctly appear in landscape orientation, too. 使用这种方法,当您以横向显示控制器时,它也将正确以横向显示。

I had the same problem. 我有同样的问题。 You have to implement shouldAutorotateToInterfaceOrientation: for your UITabBarController and return YES for all the orientations you want to support across all your view controllers. 您必须为UITabBarController实现shouldAutorotateToInterfaceOrientation: UITabBarController并为要在所有视图控制器中支持的所有方向返回YES。 And in the same method for all the other view controllers return YES only for the orientations you want to support in each of those view controllers. 并且对于所有其他视图控制器,使用相同的方法仅对要在每个视图控制器中支持的方向返回YES。

To implement shouldAutorotateToInterfaceOrientation: for your UITabBarController, you could subclass UITabBarController, but an easier way is to implement a category on UITabBarController and just implement that method: 要实现shouldAutorotateToInterfaceOrientation:对于UITabBarController,可以将UITabBarController子类化,但是一种更简单的方法是在UITabBarController上实现类别,然后仅实现该方法:

@implementation UITabBarController(orientation)

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation
{
    return YES; // or whatever you need
}

@end

NB: Typed into a web browser; 注意:输入网络浏览器; not compiled. 没有编译。 :-) :-)

You can put this right in your app delegate .m file at the end. 您可以在最后将它放在您的应用程序委托.m文件中。

What I found was that UITabBarController returns YES only for portrait, which apparently also affects all the subordinate view controllers. 我发现UITabBarController仅对肖像返回YES,这显然也会影响所有从属视图控制器。 In my case I had one subordinate view controller that I wanted to support both portrait and landscape and this solved it. 以我为例,我有一个下级视图控制器,我想同时支持纵向和横向,这解决了这一问题。

If you have the navigation inside of a tab bar, then the only option is to present the particular view as a modal view (tab bar requires all views to support the orientation to autorotate). 如果导航栏位于选项卡栏的内部,则唯一的选择是将特定视图显示为模式视图(选项卡栏要求所有视图都支持自动旋转的方向)。 So basically the single view controller, that needs landscape should implement 所以基本上需要景观的单视图控制器应该实现

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
      if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
          // Create the landscape version of the view and present it modally
      }

      return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

Then dismiss the modal view in the modal view controller's same method, by detecting portrait orientation and using the method for the parent [self.parentViewController dismissModalViewControllerAnimated: animated] 然后,通过检测肖像方向并将其用于父级,以模态视图控制器的相同方法关闭模态视图[self.parentViewController dismissModalViewControllerAnimated: animated]

For iOS-6 , I have done this , it is running greatly 对于iOS-6,我已经做到了,它运行得很好

(NSUInteger)supportedInterfaceOrientations
{ 
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}

(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{ 
    return UIInterfaceOrientationLandscapeLeft;
}

This will help you.... 这将为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM