美文网首页
iOS 屏幕旋转

iOS 屏幕旋转

作者: 永歌森林 | 来源:发表于2017-09-06 10:19 被阅读41次

    第一种

    xocde中选中此选项

    在基类控制器,RootTableViewController,RootNaviViewController,RootViewController中添加如下

    --> RootTableViewController
    - (BOOL)shouldAutorotate
    {
        UIViewController *vc = self.viewControllers[self.selectedIndex];
        if ([vc isKindOfClass:[UINavigationController class]]) {
            UINavigationController *nav = (UINavigationController *)vc;
            return [nav.topViewController shouldAutorotate];
        } else {
            return [vc shouldAutorotate];
        }
    }
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations
    {
        UIViewController *vc = self.viewControllers[self.selectedIndex];
        if ([vc isKindOfClass:[UINavigationController class]]) {
            UINavigationController *nav = (UINavigationController *)vc;
            return [nav.topViewController supportedInterfaceOrientations];
        } else {
            return [vc supportedInterfaceOrientations];
        }
    }
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        UIViewController *vc = self.viewControllers[self.selectedIndex];
        if ([vc isKindOfClass:[UINavigationController class]]) {
            UINavigationController *nav = (UINavigationController *)vc;
            return [nav.topViewController preferredInterfaceOrientationForPresentation];
        } else {
            return [vc preferredInterfaceOrientationForPresentation];
        }
    }
    
    --> RootNaviViewController
    - (BOOL)shouldAutorotate
    {
        return self.topViewController.shouldAutorotate;
    }
    
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations
    {
        return self.topViewController.supportedInterfaceOrientations;
    }
    
    // 默认的屏幕方向(当前ViewController必须是通过模态出来的UIViewController(模态带导航的无效)方式展现出来的,才会调用这个方法)
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        return [self.topViewController preferredInterfaceOrientationForPresentation];
    }
    
    --> RootViewController
    - (BOOL)shouldAutorotate {
        return NO;
    }
    
    // 支持哪些屏幕方向
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskPortrait;
    }
    
    // 默认的屏幕方向(当前ViewController必须是通过模态出来的UIViewController(模态带导航的无效)方式展现出来的,才会调用这个方法)
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        return UIInterfaceOrientationPortrait;
    }
    在继承RootViewController 中添加重写以上方法
    
    如果A屏幕是横屏,跳到B屏幕为竖屏,需要强制转屏
    - (void)interfaceOrientation:(UIInterfaceOrientation)orientation
    {
        if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
            SEL selector             = NSSelectorFromString(@"setOrientation:");
            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
            [invocation setSelector:selector];
            [invocation setTarget:[UIDevice currentDevice]];
            int val = orientation;
            [invocation setArgument:&val atIndex:2];
            [invocation invoke];
        }
    }
    
    

    第二种

    xcode中没有勾选此项

    在APPDelegate中添加以下方法与属性.在想要旋转的控制器里,更改" isEnable "
    的值.

    @property (assign, nonatomic) BOOL  isEnable;
    
    - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        if (self.isEnable) {
            return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
        } else {
            return UIInterfaceOrientationMaskPortrait;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:iOS 屏幕旋转

          本文链接:https://www.haomeiwen.com/subject/omemjxtx.html