开发者

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 t开发者_开发技巧hose 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....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜