1

I enabled the landscape orientations for my app at the general target settings: enabled landscape orientations

This is all working fine - the app is available in all selected orientations...

Now I want to disable the landscape mode on only one single view.

I tried the following at the specific view controller:

- (BOOL)shouldAutorotate
{
    return NO;
}


- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

Unfortunately without success...


Edit:

The structure of my app is:

UITabBarController
    UINavigationController
        UITableViewController
            UIViewController
Laurenz Glück
  • 1,762
  • 3
  • 17
  • 38

1 Answers1

1

Did you put the code above in the ViewController of the view or the NavigationController? You need to put it in the navigation controller, otherwise the navigation controller will rotate, causing the view to rotate as well. Subclass UINavigationController and override shouldAutoRotate:

- (BOOL)shouldAutorotate
{
    id currentViewController = self.topViewController;

    if ([currentViewController isKindOfClass:[ViewController class]])
        return NO;

    return YES;
}

Consequently, the navigation controller will only rotate if the displayed view is the view you do not want to rotate.

EDIT

Inside UITabBarController:

- (BOOL)shouldAutorotate
{

    if([self.selectedViewController isKindOfClass:[UINavigationController class]]){

        UINavigationController *navigationController = (UINavigationController *) self.selectedViewController;
        id currentViewController = navigationController.topViewController;

        if ([currentViewController isKindOfClass:[ViewController class]])
            return NO;
        }
    }
    return YES;
}
Josh Gafni
  • 2,831
  • 2
  • 19
  • 32
  • Thanks for you quick response! Added your code to the NavigationController - but nothing happened... I added my app structure to the question - maybe there is a problem? – Laurenz Glück Mar 14 '15 at 21:01
  • I added a breakpoint to the shouldAutorotate function and it is not called if I open up the tableview (in the tab)... – Laurenz Glück Mar 14 '15 at 21:03
  • Actually hold on. What exactly do you want to happen. Do you want the Tabs to rotate, but the view inside it to not? Do you want the navigation bar to rotate as well? – Josh Gafni Mar 14 '15 at 21:04
  • I have different tabs, inside of them are some tableviews, below the tableviews are normal view controller... The "normal" app should rotate, but some of the "sub views" below the tableview should not... – Laurenz Glück Mar 14 '15 at 21:10
  • but i imagine when you are viewing the "Normal view controllers" that the tabs and the navigation bar are still visible on the screen. Do you want those to not rotate as well if the "Normal view controller" is on the screen? – Josh Gafni Mar 14 '15 at 21:13
  • If the "Normal view controllers" are on the screen, the app should rotate completely (with tab / nav bar...). Only if the specific "detail view" appears (tab bar is now hidden) the app should not rotate anymore... – Laurenz Glück Mar 14 '15 at 21:16
  • If I set "shouldAutorotate {return NO}" to the UITabBarController the app isn't rotating anymore... Is there a way to check for something like "isKindOfClass"...? You know what I mean? – Laurenz Glück Mar 14 '15 at 21:17
  • See the answer here: http://stackoverflow.com/questions/4306704/uitabbarcontroller-how-to-access-a-view-controller. Subclass UITabBarController, and put the override the shouldAutoRotate. You need to get the displayed navigation controller and check if its topViewController is the detailViewController. If so, return NO. – Josh Gafni Mar 14 '15 at 21:21
  • `UIViewController *currentVC = self.tabBarController.selectedViewController;` at the UITabBarController is returning null inside of the shouldAutorotate function - you know why? – Laurenz Glück Mar 14 '15 at 21:41
  • why are you referencing self.tabBarController inside UITabBarController? self is a UITabBarController... – Josh Gafni Mar 14 '15 at 21:50
  • I added this code to my **UITabBarController** - isn't that right? – Laurenz Glück Mar 14 '15 at 21:53
  • If I log `currentViewController` it is `null` all the time - if I check for `self.selectedViewController`, I get the current UINavigationController - really weird... – Laurenz Glück Mar 14 '15 at 22:11
  • now I am getting an error `topViewController` is not found on `selectedViewController` ... :/ – Laurenz Glück Mar 14 '15 at 22:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73002/discussion-between-josh-gafni-and-laurenz-gluck). – Josh Gafni Mar 14 '15 at 22:18