美文网首页
iOS横竖屏切换

iOS横竖屏切换

作者: 毅个天亮 | 来源:发表于2017-07-12 11:32 被阅读77次

    导航控制器PUSH

    需求:当前竖屏下情况下,导航栏直接PUSH出一个横屏的控制器,并在POP后返回竖屏状态。
    效果图:

    push.gif

    主要实现是通过下面这两步:
    ①. 让AppDelegate在屏幕方向相关的代理方法中,返回我们期望的屏幕方向

    // AppDelegate.h中
    typedef NS_ENUM(NSUInteger, ScreenOrientation) {
        PortraitMode = 0,
        LandscapeMode,
        AllButUpsideDownMode,
    };
    
    @property (nonatomic, assign) ScreenOrientation viewRotateMode;
    
    // AppDelegate.m中
    -(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        
        if (self.viewRotateMode == 0) {
            return UIInterfaceOrientationMaskPortrait;
        } else if (self.viewRotateMode == 1) {
            return UIInterfaceOrientationMaskLandscape;
        }
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        if (self.viewRotateMode == 0) {
            return UIInterfaceOrientationMaskPortrait;
        } else if (self.viewRotateMode == 1) {
            return UIInterfaceOrientationMaskLandscape;
        }
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    

    ②. 通过KVC改变UIDeviceorientation属性

    NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
    

    具体实现:
    在被push的ViewController中:

    -(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        AppDelegate *delegate =(AppDelegate *) [UIApplication sharedApplication].delegate;
        delegate.viewRotateMode = LandscapeMode;
        NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
        [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
        
        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        AppDelegate *delegate =(AppDelegate *) [UIApplication sharedApplication].delegate;
        delegate.viewRotateMode = PortraitMode;
        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
    }
    

    模态,单个控制器交互后横竖屏切换

    一、重载根控制器

    @property(nonatomic, readonly) UIInterfaceOrientationMask supportedInterfaceOrientations
    在这个属性的文档中有如下解释:
    When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that fills the window. 
    

    iOS 6之后,控制器的方向由根控制器管理,可使用继承或者分类的方式,重载根控制器关于旋转屏幕的三个方法,将方向的选择权归还给子控制器。

    @implementation UINavigationController (manualRotate)
    
    - (BOOL)shouldAutorotate {
        return self.topViewController.shouldAutorotate;
    }
    
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
        return self.topViewController.supportedInterfaceOrientations;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        return self.topViewController.preferredInterfaceOrientationForPresentation;
    }
    
    @end
    
    
    @implementation UITabBarController (manualRotate)
    //在`iOS 5`及更早的版本中默认返回`NO`, 其他默认返回`YES`
    - (BOOL)shouldAutorotate {
        return self.selectedViewController.shouldAutorotate;
    }
    
    // shouldAutorotate为Yes时才会被调用
    // iPad默认返回UIInterfaceOrientationMaskAll
    // iPhone默认返回UIInterfaceOrientationMaskAllButUpsideDown
    // 此方法的返回值与AppDelegate的application:supportedInterfaceOrientationsForWindow:的交集才是有效值
    // AppDelegate的application:supportedInterfaceOrientationsForWindow:会覆盖工程配置中(Info.plist)的Supported interface orientations
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
        return self.selectedViewController.supportedInterfaceOrientations;
    }
    
    // 系统会默认按statusBar的方向来展示一个新的控制器,即默认按当前方向
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        return self.selectedViewController.preferredInterfaceOrientationForPresentation;
    }
    
    @end
    

    至此,新的控制器,只需要重载上面的方法就能管理自己的方向了。

    二、强制修改设备方向

    @implementation UIDevice (manualRotate)
    + (void)setOrientation:(UIInterfaceOrientation)orientation {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[self currentDevice]];
        NSInteger val = orientation;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
    @end
    

    三、使用

    1. 父控制器为竖屏,子控制器为横屏,只需要在自控制器重写三个方法即可。注意需要控制好父、子控制器之间方向的关系。

    2. 同一个控制器,需要在竖屏和横屏之间切换,如带有视频播放的控制器。需要重写上面的方法,然后调用[UIDevice setOrientation: orientation] 方法进行屏幕旋转。

      这里写图片描述

    四、布局

    同一控制器在横竖屏之间切换,经常需要不同的布局。在iOS 8及以后的系统可以使用SizeClass方便地部署横竖屏的AutoLayout。在Xcode 8及更高版本后,界面有所改变。

    这里写图片描述

    所有不是在Vary for Traits 下添加的约束会被当做为所有尺寸的设备添加的约束。
    点击Vary for Traits 选择宽高,可以为指定的设备(群)添加约束。

    五、方向说明

    UIDeviceOrientation 用于表示设备的物理方向

    typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
        UIDeviceOrientationUnknown,
        UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
        UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
        UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
        UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
        UIDeviceOrientationFaceUp,              // Device oriented flat, face up
        UIDeviceOrientationFaceDown             // Device oriented flat, face down
    } __TVOS_PROHIBITED;
    

    UIInterfaceOrientation 用于表示界面的方向,当设备向右旋转时,UI需要向左旋转,所以在LeftRight两个方向上,UIInterfaceOrientationUIDeviceOrientation是相反的。

    typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
        UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
        UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
        UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
        UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
        UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
    }
    

    UIInterfaceOrientationMask ,在单个方向上与UIInterfaceOrientation并无区别,只是多了UIInterfaceOrientationMaskLandscapeUIInterfaceOrientationMaskAllUIInterfaceOrientationMaskAllButUpsideDown等表示界面方向的集合。

    typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
        UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
        UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
        UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
        UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
        UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
        UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
        UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    }
    

    相关文章

      网友评论

          本文标题:iOS横竖屏切换

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