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 .

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,110评论 0 23
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa阅读 8,947评论 0 6
  • 01 女人要有单身的能力 闺蜜优优最近和老公的感情出现了问题,正在闹离婚。 她问我:“你和老公周末才见面,你就不怕...
    吉马妈妈Anna阅读 982评论 0 0
  • 老爸老妈28周年的结婚纪念日,一时兴起想为他们写点东西。我一直觉得我是个极其幸运的人,因为我有最好的爸妈,我所深爱...
    林慕阅读 418评论 0 0
  • 是夜,夜已深沉 昏暗的房间里女人安然沉睡在床上,突然她感到身体被人紧紧抱住,她知道是她最爱的男人,因为平时睡觉他也...
    莫艺非凡阅读 147评论 0 0