1

I have overridden my UINavigationController to implement these methods:

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

Now from my first view, I modally present this MSNavigationPaneViewController here which is like a Facebook side sliding tab bar controller.

I need 1 of my view controllers that are displayed within here to be displayed Landscape only while the rest of the app's views are Portrait only.

So in all my other View Controllers I have added:

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
    return UIInterfaceOrientationMaskPortrait;
}

and in the one that I want Landscape i've added:

- (BOOL)shouldAutorotate
{
    return UIInterfaceOrientationMaskLandscape;
}
-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

However all my views rotate.

If I change shouldRotate to NO on these other views then they stay in Portrait and the view I want to be Landscape can be rotated, however I can't get it to rotate itself. Also, once rotated if I go back to another view that has shouldRotate = NO; then it can't be rotated back to Portrait.

I've been at this for hours now and can't get it to work.

Thanks

EDIT -----

I have this half working now and have started a new question to get the auto rotation working here

Community
  • 1
  • 1
Darren
  • 10,182
  • 20
  • 95
  • 162

3 Answers3

5

It seems you are confusing what to return in shouldAutorotate. Use the following code as an example.

Since iOS is making a call into your app's shouldAutorotate in response to an event from the accelerometer, it already knows the new orientation; if your app answers 'YES`, iOS could then check the current orientation against the list of supported ones, and come up with a decision without your app querying for the current orientation.

// iOS 6

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

Also, You can let the view controller that presents your modal view controller inform it of rotation. Use: presentViewController:animated:completion: to present the view controller. presentModalViewController:animated: is deprecated if by chance you are using it.

If you add application:supportedInterfaceOrientationsForWindow:, make sure to follow the code guideline below. The documentation states that if you implement supportedInterfaceOrientations on a VC it should override the app delegate. However, people have noticed it makes a difference as to how you add the rootViewController to the main window.

Use:

window.rootViewController = viewController

instead of:

[window addSubview:viewController.view];
Bejmax
  • 945
  • 7
  • 16
  • Thanks, I'll double check everything again tomorrow – Darren Dec 18 '12 at 00:26
  • Thanks. This much is working now and each view can only be rotated to the mask set for the view, however I can't get PreferredOrientation to fire as I dont think the MSNavigationPaneViewController 'presents' each view, it just swaps them out. – Darren Dec 18 '12 at 09:23
  • I've started a new question with regards to the auto rotation which I can't get working. http://stackoverflow.com/questions/13934807/trick-preferredinterfaceorientationforpresentation-to-fire-on-viewcontroller-cha – Darren Dec 18 '12 at 14:18
  • window.rootViewController = viewController is definitely needed for ios 6 instead of [window addSubview:viewController.view]; Thank you so much! – Raymond Wang Jan 16 '13 at 22:17
2

I did this in this way

-(BOOL) shouldAutorotate{
    return YES;
}

-(NSUInteger) supportedInterfaceOrientations{
    if ([self.visibleViewController isKindOfClass:[ZoomPictureViewController class]]) {
        return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait;
}

Make sure you've clicked the orientation buttons in Xcode to enable the orientations you want and return the orientations in supportedInterfaceOrientations method.

ader
  • 5,403
  • 1
  • 21
  • 26
fibnochi
  • 1,113
  • 1
  • 9
  • 26
1

First, make sure you've clicked the orientation buttons in Xcode to enable the orientations you want. Then spend a few hours Googling all the bugs and problems people have encountered with iOS 6. And a few more hours experimenting with various techniques.

This post contains a few clues, but is far from the final word in the matter.

Note in particular that you now must identify the "root view controller" and concentrate most (but not all) of the rotation controls there, vs implementing it in each view controller.

Community
  • 1
  • 1
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • Thanks, I'll go through all this again. If I present a modal view, does that still need the rotation controls from the rootView? – Darren Dec 18 '12 at 00:25
  • @Darren -- I don't think anyone's figured out modal view rotation in iOS 6 yet. – Hot Licks Dec 18 '12 at 00:50
  • @HotLicks i think the only way is to have the VC that presents the modal VC inform it of rotation. Updated my answer to mention this. – Bejmax Dec 18 '12 at 01:05
  • Thanks, I have the allowed rotations working now. I've started a new question with regards the autorotation http://stackoverflow.com/questions/13934807/trick-preferredinterfaceorientationforpresentation-to-fire-on-viewcontroller-cha – Darren Dec 18 '12 at 14:18