Handling autorotation for one view controller in iOS7

前端 未结 5 1432
野趣味
野趣味 2020-12-06 02:37

I\'ve read many answers on SO but I can\'t seem to get autorotation working on iOS7.

I only need one view controller to rotate, so I don\'t want to set rotation sett

相关标签:
5条回答
  • 2020-12-06 03:03

    In my case, I had a new iOS7 app with about 30 view controllers created already. I needed auto rotation on just a single modal view controller. I didn't want to have to update the preexisting view controllers.

    I selected the orientations I wanted in the plist:

    select orientations

    Then I added a category to my app delegate on UIViewController:

    @implementation UIViewController (rotate)
       -(BOOL)shouldAutorotate {
          return NO;
       }
    @end
    

    Then in the single modal view controller I WANTED to rotate I added this method:

    -(BOOL)shouldAutorotate {
          return YES;
    }
    

    I also discovered, that if my view controller wasn't a modal VC I would need to add category methods on UINavigationController instead, for all VCs that were subsequent to the root view controller, as part of the navigation stack of view controllers - similar to this: https://stackoverflow.com/a/20283331/396429

    0 讨论(0)
  • 2020-12-06 03:04

    I know this is old but I ended up in a more unique situation where we have 50+ ViewController all over the app that I refused to go through and modify and support the same orientation in all of them but one or 2. Which brings me to my answer. I created a UIViewController category that overrides - (BOOL)shouldAutorotate to always return NO or YES depending on device type etc. (this can be done with supported interface orientations too). Then on the ViewControllers I wanted to support more then just portrait, I swizzled shouldAutorotate to return YES. Then forced the orientation change when the view is dismissed on the parent ViewControllers viewWillAppear method using:

    [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"].

    When all was said and done, I accomplished everything I wanted on a few ViewControllers with < 30 lines of code using a macro for swizzling. Had I done it by replacing shouldAutorotate and supportedInterfaceOrientations on all of the VC's in the application I would have ~250 extra lines of code. and a lot of grunt work adding it in the first place.

    0 讨论(0)
  • 2020-12-06 03:06

    You need to set the plist value to all possible values, then limit them as you see fit (in the Navigation Controllers and TabBar Controllers. From the UIViewController class description:

    In iOS 6 and later, your app supports the interface orientations defined in your app’s Info.plist file. A view controller can override the supportedInterfaceOrientations method to limit the list of supported orientations. 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; 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. The intersection of the app’s orientation mask and the view controller’s orientation mask is used to determine which orientations a view controller can be rotated into.

    0 讨论(0)
  • 2020-12-06 03:07

    I've faced such problem - had only one landscape view in my app. I've used below code to to handle that.

    #import "objc/message.h"
    -(void)viewWillAppear:(BOOL)animated{
    
    objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft);
    }
    
    0 讨论(0)
  • 2020-12-06 03:17

    Simple but it work very fine. IOS 7.1 and 8

    AppDelegate.h

    @property () BOOL restrictRotation;
    

    AppDelegate.m

    -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
    if(self.restrictRotation)
        return UIInterfaceOrientationMaskPortrait;
    else
        return UIInterfaceOrientationMaskAll;
    }
    

    ViewController

    -(void) restrictRotation:(BOOL) restriction
    {
        AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        appDelegate.restrictRotation = restriction;
    }
    

    viewDidLoad

    [self restrictRotation:YES]; or NO
    
    0 讨论(0)
提交回复
热议问题