MarkPowell
MarkPowell

Reputation: 16530

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?

Upvotes: 2

Views: 3326

Answers (4)

user1920669
user1920669

Reputation: 11

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

(NSUInteger)supportedInterfaceOrientations
{ 
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}

(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{ 
    return UIInterfaceOrientationLandscapeLeft;
}

This will help you....

Upvotes: 0

Henri Normak
Henri Normak

Reputation: 4725

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]

Upvotes: 1

Mark Granoff
Mark Granoff

Reputation: 16938

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.

Upvotes: 1

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

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.

Upvotes: 3

Related Questions