1

I'm embedding my app in a UINavigationController, I want most of myViewControllers except one to be Portrait, I've read a lot of questions but could not find a correct answer that works for me.

In my target I'm selecting Device Orientation : Portrait, Landscape Right

I'm adding this to my first ViewController:

-(BOOL)shouldAutorotate{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations{
    return (UIInterfaceOrientationPortrait);
}

But when I rotate the device left the ViewController rotates as well. Why is it rotating?

Clad Clad
  • 2,653
  • 1
  • 20
  • 32
Eli Braginskiy
  • 2,867
  • 5
  • 31
  • 46

1 Answers1

1

You can't easily do in iOS 7 what you're describing. A UINavigationController does not consult its children as to what rotations they like; whatever the permitted rotations of the UINavigationController, those are the permitted rotations of the app, regardless of which child happens to be showing at that moment.

The only really legal and built-in way to force rotation is to use a presented ("modal") view controller that takes over the screen. Its rotation settings are consulted because it is now in charge of the screen.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks matt. How about subclassing the `UINavigationController` and adding a stub method into the `ViewController` to tell the `UINavigationController ` which orientation to use?. P.S. Since you answer most of my questions I herby declare you my personal assistant. – Eli Braginskiy Mar 20 '14 at 16:12
  • :) What you are describing doesn't work because the navigation controller is not consulted a second time just because it pushed or popped a view controller. See http://stackoverflow.com/questions/15300819/in-ios6-trouble-forcing-viewcontroller-to-certain-interfaceorientation-when-pus/16379515#16379515 for a very skanky workaround. – matt Mar 20 '14 at 16:15
  • But really, don't do that. Rethink your interface instead. Really. – matt Mar 20 '14 at 16:18
  • I really have to have one of my ViewControllers Landscape only. Do you suggest to use a modal segue then? I need all of my other ViewControllers to be Portrait only. If I use a modal segue I can force the ViewController to be landscape only? and the other ViewControllers to be Portrait only? – Eli Braginskiy Mar 20 '14 at 16:22
  • Absolutely yes. See my discussion here: http://www.apeth.com/iOSBook/ch19.html#_rotation_of_a_presented_view It's the reverse of what you describe: we are usually landscape, but this one view controller is to be portrait. So just reverse the values and there you are. – matt Mar 20 '14 at 16:31
  • Oh, one more thing: to set the permitted orientations for the navigation controller, use this new delegate method: https://developer.apple.com/library/ios/documentation/uikit/reference/UINavigationControllerDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UINavigationControllerDelegate/navigationControllerSupportedInterfaceOrientations: – matt Mar 20 '14 at 16:33