简体   繁体   中英

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

I have a situation where I have multiple views controlled via UITabBarController . One of those views is controlled via a 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:

- (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. 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.

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:

@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.

What I found was that UITabBarController returns YES only for portrait, which apparently also affects all the subordinate view controllers. 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]

For iOS-6 , I have done this , it is running greatly

(NSUInteger)supportedInterfaceOrientations
{ 
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}

(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{ 
    return UIInterfaceOrientationLandscapeLeft;
}

This will help you....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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