iOS 6.0 restrict auto rotation within a navigation controller?

后端 未结 2 1240
南笙
南笙 2021-01-07 04:44

What else should I do?

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) toInterfaceOrientation
{
    return (toInterfaceOrientation ==          


        
2条回答
  •  萌比男神i
    2021-01-07 05:34

    ORIGINAL ANSWER: No need to subclass - just do a category like I described in my solution here: Top-home button portrait orientation in iOS6 simulator not working

    Basically, for iPhone the UINavigationController allows rotation for everything except "top home button portrait", for iPad it allows everything.

    So either you do a category forwarding the decision to the currently active view controller or something static like

    UINavigationController-Rotation.h:

    @interface UINavigationController (Rotation)
    @end
    

    UINavigationController-Rotation.m:

    #import "UINavigationController-Rotation.h"
    
    @implementation UINavigationController (Rotation)
    
    #pragma From UINavigationController
    
    - (BOOL)shouldAutorotate {
    
        return NO;
    }
    
    - (NSUInteger)supportedInterfaceOrientations {
    
        return UIInterfaceOrientationMaskPortrait;
    }
    
    #pragma -
    
    @end
    

    UPDATE: As Javier Soto pointed out, this might lead to undefined behavior if there is a second category doing the same. In that case, subclassing might be a better solution.

    In a situation where you know there is no other category doing the same I still consider this a working, low effort, local and pragmatic solution. I am not religious about that. Decide yourself.

提交回复
热议问题