iOS 6 - Navigation Controller Landscape Rotations For Some Views While Others Portrait Only

别来无恙 提交于 2019-12-02 00:50:33

问题


I am building an app that will be Portrait only for the main views (Both normal and upside down). I have set this setting in the Project Settings/Plist and all works fine. However, I have a few modal views that do things like display images/videos, and I want them to be able to rotate to ALL orientations.

I tried adding a category for UINavigationController but no luck. I have also added to the viewController that calls the modal the below code:

-(BOOL)shouldAutomaticallyForwardAppearanceMethods{
    return NO;
}
-(BOOL)shouldAutomaticallyForwardRotationMethods{
    return NO;
}

I have added the below code to the modal viewControllers that I want to allow all orientations:

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

What am I missing? Any suggestions?


回答1:


On iOS 6, rotation handling has changed. For the problem you described, there are several approaches. First, in the plist, enable all orientations except portrait upside down.

Then you could use one of the following solutions:

  1. Use a subclass of UINavigationController that does only allow rotation to portrait.
  2. For all controllers specify which rotations they support.
  3. You can override a method in your application delegate. As that method is called on your delegate whenever the orientation changes or a new view controller is pushed, you can use it to temporarily enable/disable landscape display for your app:

    // In AppDelegate.h:
    @property (nonatomic) BOOL portraitOnly;
    
    // In AppDelegate.m:
    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        return _portraitOnly ? UIInterfaceOrientationMaskPortrait : UIInterfaceOrientationMaskAllButUpsideDown;
    }
    
    // to switch to portrait only:
    ((AppDelegate *)[UIApplication sharedApplication].delegate).portraitOnly = YES;
    
    // to switch to allowing landscape orientations:
    ((AppDelegate *)[UIApplication sharedApplication].delegate).portraitOnly = NO;
    



回答2:


You need to enable rotation for the viewControllers that are ancestors in the hierarchy in your build settings (in order to allow the VC's lower in the hierarchy to be able to rotate if they want to). And then return the proper logic from shouldAutoRotate, supportedInterfaceOrientations,and willAnimateRotationToInterfaceOrientation in each viewController.

-Return NO from shouldAutoRotate for the viewControllers that do not move at all.

-And likewise, return YES from shouldAutoRotate and return the orientations valid in that VC from supportedInterfaceOrientations

-Use willAnimateRotationToInterfaceOrientation to do any last minute cleanup or data changes you need to do before the new orientation appears.

Feel free to let me know if your still having trouble. Hope it helps!



来源:https://stackoverflow.com/questions/14989353/ios-6-navigation-controller-landscape-rotations-for-some-views-while-others-po

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!