iOS - How to restrict some specific to portrait or landscape mode ?

iOS - How to restrict some specific to portrait or landscape mode ?

Sometimes, you need to disable some specific view controllers to portrait or landscape mode. This is simple . All you need to to is to implement

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

method in Appdelegate.m.

Let's say in my app , there is only StockViewController can be showed as landscape mode.

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    //only stockviewcontroller can be showed in landscape view
    UITabBarController *rootVC = (UITabBarController *)window.rootViewController;
    UINavigationController *rootNavi = (UINavigationController *)rootVC.selectedViewController;
    UIViewController *presentedVC = rootNavi.viewControllers[rootNavi.viewControllers.count - 1];
    if ([presentedVC isKindOfClass:[StockViewController class]]) {
        return UIInterfaceOrientationMaskAll;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

As you can see , all you need to do is to find out the view controller that is presented by the window's root.rootViewController, and return the orientation you want .

你可能感兴趣的:(iOS - How to restrict some specific to portrait or landscape mode ?)