美文网首页
iOS 页面旋转小结

iOS 页面旋转小结

作者: 猴子的饼干 | 来源:发表于2018-08-27 13:03 被阅读50次

    近期项目中需要对部分页面进行可旋转的控制,本篇文章算是对开发过程的一个总结。为何这么简单的东西要总结?还不是一直没做过么········[脸红.jpg]

    首先我们新建一个【Single View App】项目开始吧。

    相关的配置

    要让项目支持旋转首先得在info.plist文件中进行如下设置:

    image

    图中下方的为iPad设备上支持的方向,因我们创建项目的时候没有指定设备,缺省设备就有iPad设备的方向。

    当然,简单直接点就在targets中配置,效果相同:


    image

    默认情况下,项目已经勾选了三种设备方向,其中【Upside Down】为Home Button在上方,默认不勾选。

    此时运行项目并旋转设备,能看到页面可以左右旋转,但不能倒立旋转(因为我们没有勾选Upside Down啊!) 。

    Tips: 模拟器旋转设备的方法是“cmd + 左\右方向键”

    自定义旋转

    当前的App旋转是基于配置文件的设置,整个APP都能够旋转。我们要实现手动控制的话还得先看UIViewController中的有三个方法:

    //是否能够旋转
    - (BOOL)shouldAutorotate;
    //支持的旋转方向
    -(UIInterfaceOrientationMask)supportedInterfaceOrientations;
    //模态视图初始化时的旋转方向
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
    

    现在在ViewController中实现前两个方法

    - (BOOL)shouldAutorotate {
        return YES;
    }
    
    -(UIInterfaceOrientationMask)supportedInterfaceOrientations{
        return UIInterfaceOrientationMaskAll;
    }
    
    

    运行后发现,并不能实现倒转屏幕(Upside Down),这是因为我们在配置文件中并没有勾选它,所以这里的自定义所支持的方向必须是配置文件中所支持的。我们将“UIInterfaceOrientationMaskAll”修改为“UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft”后再运行,发现这时候仅支持LandscapeLeft与Portrait两个方向,达到了我们所设置的效果。

    优化状态栏

    到目前为止我们的程序已经可以自定义旋转了,不过有点小瑕疵...旋转屏幕至横屏的时候,状态栏消失了。在网上找到如下方法:

    在info.plist文件中 配置“View controller-based status bar appearance”为“NO”。
    并在AppDelegate中的didFinishLaunchingWithOptions里添加如下代码:

        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    

    这样就能在旋转时保持状态烂显示。

    但总觉得这个方法有点奇怪,不仅将info中的配置为NO,意思对不上,还在AppDelegate里面又是隐藏又是显示的,究竟要作甚啊??

    我们点击方法进去看到此方法在iOS9就不推荐使用了,替代方法为[UIViewController prefersStatusBarHidden]。没准苹果也觉得难受了吧。

    使用该方法需要将info.plist中的“View controller-based status bar appearance”设置成“YES”,然后在ViewController中实现prefersStatusBarHidden 返回NO。

    - (BOOL)prefersStatusBarHidden {
        return NO;
    }
    

    再运行,完美达成效果,这样设置的话还能单独控制状态栏的显示,比之前的方式好多了。

    *****[重要]项目中要注意项目的结构

    前一部分仅仅是一个测试性的Demo,现在我们把这个Demo复杂化一点,模仿真实的项目结构,例如下面这种结构:


    image

    我们将之前控制单个页面的方式使用到这里,这时候会发现,项目这里有两个页面,你得重写两次相关方法,且真实的项目中会有几十上百个页面,所以我们得将重写的方法放入基类中。

    假设我们的项目只有个别页面需要旋转,我们在基类中默认页面为不可旋转,需要旋转的页面复写相关方法即可。这里就不再赘述相关代码。

    屏幕旋转是从全屏视图的方法来判定的,即root视图来判定的。也就是说每次判定页面是否需要旋转都是从图中最左侧的nav判定。我们要做到每个页面自定义的话需要在UINavigationController和UITabbarController中分别实现如下方法:

    //BaseNavgationController
    - (BOOL)shouldAutorotate {
        NSLog(@"%@", [self class]);
        return self.topViewController.shouldAutorotate;
    }
    
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
        return self.topViewController.supportedInterfaceOrientations;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        return [self.topViewController preferredInterfaceOrientationForPresentation];
    }
    
    //BaseTabbarController
    - (BOOL)shouldAutorotate {
        NSLog(@"%@", [self class]);
        return self.selectedViewController.shouldAutorotate;
    }
    
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
        return self.selectedViewController.supportedInterfaceOrientations;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        return self.selectedViewController.preferredInterfaceOrientationForPresentation;
    }
    

    上述方法只是调用了下级页面的旋转设置,这样就能够使我们在各ViewController中的设置起到效果。

    Tips:笔者在可旋转的多级后的子页面页面pop返回根视图时,不可旋转的根视图居然旋转了,调试也没发现哪里修改到了根视图的旋转状态,木有找到旋转的内部机制资料[尴尬.jpg],笔者只得手动的旋转屏幕方向,使用如下方法:

    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 
    

    相关文章

      网友评论

          本文标题:iOS 页面旋转小结

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