iOS强制转屏

作者: wnido | 来源:发表于2017-04-05 17:15 被阅读506次

    引言

    遇到需要转屏的功能,查资料刚好看到一篇iOS强制横屏或强制竖屏,不过发现里面的方法有点太不可取,做了下修改。


    为什么push或者pop后的viewController 不响应所有的 旋转方法?

    这里是因为,根视图(rootController)不是你的viewController,你去监听肯定是没有的,这里你就需要监听根视图,比如说下面的例子中的nav。


    BaseNavCtl ---- 这代码里,是为了让MultiQualityViewController横屏,这里就举这一个栗子

    #import <UIKit/UIKit.h>
    
    @interface BaseNavCtl : UINavigationController
    
    @end
    

    BaseNavCtl .m

        //
        //  BaseNavCtl.m
        //  MGMediaDemo
        //
        //  Created by ciome on 2017/4/5.
        //  Copyright © 2017年 MiGu. All rights reserved.
        //
        
        #import "BaseNavCtl.h"
        #import "MultiQualityViewController.h"
        
        @interface BaseNavCtl ()
        
        
        @end
        
        @implementation BaseNavCtl
        
        - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
        
        // 获取topCtl
        UIViewController  *ctl = self.topViewController;
        // 进行后续区分
        if([ctl isKindOfClass:[MultiQualityViewController class]]){
            
            return UIInterfaceOrientationMaskLandscape;
        }
        else {
            
            return UIInterfaceOrientationMaskAll;
        }
    }
    
    - (BOOL)shouldAutorotate
    {
        return true;
    }
    
     - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
        // 获取topCtl
        UIViewController  *ctl = viewController;
        // 在进入之前强制旋转设备
        if([ctl isKindOfClass:[MultiQualityViewController class]]){
            
            [self interfaceOrientation:UIInterfaceOrientationLandscapeRight];
        }
        [super pushViewController:viewController animated:animated];
    }
    
    
    
    // 强制转屏 
    - (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;
            // 从2开始是因为0 1 两个参数已经被selector和target占用
            [invocation setArgument:&val atIndex:2];
            [invocation invoke];
        }
    }
    
    
    /*
    #pragma mark - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    
    @end
    

    AppDelegate --- BaseNavCtl成为rootController

    self.window= [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    BaseNavCtl *navigationController = [[BaseNavCtl alloc]initWithRootViewController:[[DemoMainViewController alloc]init]];
    self.viewController= navigationController;
    self.window.rootViewController=self.viewController;
    [self.window makeKeyAndVisible];
    

    为什么这样写

    这样写的好处是,可以把整个APP内的视图方向管理,都集中在baseNavCtl这个类里面;当然,这样也必然增加了每次匹配的消耗,但由于每次nav只有一个topViewCtl ,所以可以做唯一判别减少匹配消耗,这里就不贴代码了。

    相关文章

      网友评论

      • 89848af90932:这样管理是要好点,按你这个方法那pop的时候,是否也需要调用interfaceOrientation去强制回竖屏咯?(需求是回去保持竖屏)
        wnido:这只提供一种思路,具体使用要看你场景。 你强转横屏了,后回去,想转竖屏,肯定也要调interfaceOrientation 实现的

      本文标题:iOS强制转屏

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