Prevent autorotate for one view controller?
Asked Answered
J

3

17

My app can autorotate but I need one of the views to only show in portrait mode and don't know how to achieve this.

I tried this (among other things) but the view in question still rotates:

//  ViewController.m

-(BOOL)shouldAutorotate
{            
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

Can someone kindly point out what I'm doing wrong? Thanks.

-edit-

It's for iOS 6.1

Joust answered 21/2, 2013 at 14:7 Comment(5)
What SDK version are you developing for?Vacationist
#12630859Moores
iOS 6, sorry. I've updated the OP.Joust
Most likely you problem will be solved by CustomNavigationController. See #12630859Moores
ViewController inside NavigationController? I've met before: #14658768Melindamelinde
M
31

When a UINavigationController is involved, create a category on the UINavigationController and override supportedInterfaceOrientations.

#import "UINavigationController+Orientation.h"

@implementation UINavigationController (Orientation)

- (NSUInteger)supportedInterfaceOrientations {
   return [self.topViewController supportedInterfaceOrientations];
}

- (BOOL)shouldAutorotate {
    return YES;
}

@end  

Now, iOS containers (such as UINavigationController) do not consult their children to determine whether they should autorotate.

How to create a category
1. Add a new file (Objective c- category under cocoa touch)
2. Category : Orientation on UINavigationController
3. Add the above code to UINavigationController+Orientation.m

Mystify answered 21/2, 2013 at 14:15 Comment(4)
Yes, there is a UINavigationController involved but I have to confess to not knowing exactly what you mean by subclassing but I'll go do some reading.Joust
@Joust Just add a category on UINavigationController see my editMystify
This also applies to a parent UITabBarControllerOleate
Why not subclass and override? Apple Says: Avoid Category Method Name Clashes ..If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime. This is less likely to be an issue if you’re using categories with your own classes, but can cause problems when using categories to add methods to standard Cocoa or Cocoa Touch classes.Harmonyharmotome
W
4

Swift 3 version the accepted answer:

extension UINavigationController {

    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        // Change `.portrait` to whatever your default is throughout your app
        return topViewController?.supportedInterfaceOrientations ?? .portrait
    }

    open override var shouldAutorotate: Bool {
        return true
    }
}
Womanizer answered 28/2, 2017 at 21:18 Comment(0)
D
1

As per the documentation.

A view controller can override the supportedInterfaceOrientations method to limit the list of supported orientations.

So we need to override shouldAutorotate and supportedInterfaceOrientation to target view controllers.

Typically, the system calls this method only on the root view controller of the window or a view controller presented to fill the entire screen.

This will work if you have very simple configuration like your target view controller is the rootViewController of window or being presented covering whole screen.

In case when configuration of target view controller is complex like embedded in some other container view controllers.

child view controllers use the portion of the window provided for them by their parent view controller and no longer participate directly in decisions about what rotations are supported.

So may be default implementation of these container view controllers not asking there children for there supportedInterfaceOrientation preference.

So to allow our target child view controller to specify there supportedIntefaceOrientation we need to tell there container view controller to do so.

You can also check my previous answer here.

and Understanding UIViewController rotation when embed in Container View Controllers.

Dubose answered 25/3, 2018 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.