iOS 6 - Navigation Controller Landscape Rotations

2019-02-25 07:01发布

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?

2条回答
看我几分像从前
2楼-- · 2019-02-25 07:51

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;
    
查看更多
女痞
3楼-- · 2019-02-25 08:03

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!

查看更多
登录 后发表回答