Support different orientation for only one view iOS 6

后端 未结 7 1062
时光说笑
时光说笑 2020-11-29 03:57

I want to rotate ONLY one of my views within my app to either landscape left or landscape right. All my other views are in portrait mode and I have set my app to support onl

7条回答
  •  旧时难觅i
    2020-11-29 04:32

    UIViewController+OrientationPermissions.h

    @interface UIViewController (OrientationPermissions)
       + (void)setSupportedOrientations:(UIInterfaceOrientationMask)supportedOrientations;
       + (UIInterfaceOrientationMask)supportedOrientations;
    @end
    

    UIViewController+OrientationPermissions.m

    @implementation UIViewController (OrientationPermissions)
    static UIInterfaceOrientationMask _supportedOrientations;
    
    + (void)setSupportedOrientations:    (UIInterfaceOrientationMask)supportedOrientations {
        _supportedOrientations = supportedOrientations;
    }
    
    + (UIInterfaceOrientationMask)supportedOrientations {
        return _supportedOrientations;
    }
    
    @end
    

    In your UIApplication delegate

    - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
        return [UIViewController supportedOrientations];
    }
    

    Then on a desired view controller do something like

    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        [UIViewController setSupportedOrientations:UIInterfaceOrientationMaskAll];
    }
    

    Don't forget to reset mask before leaving this view controller

    Note, if you are using UINavigationController or UITabBarController, see https://stackoverflow.com/a/28220616/821994 how to bypass that

提交回复
热议问题