美文网首页
iOS屏幕旋转之Apple Document(苹果文档翻译)

iOS屏幕旋转之Apple Document(苹果文档翻译)

作者: 迷路的安然和无恙 | 来源:发表于2017-12-19 21:27 被阅读50次

    应用场景一:指定UIViewController支持旋转或不旋转

    实现方法如下:

    - (BOOL)shouldAutorotate {
        return YES;
    }
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskPortrait;
    }
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        return UIInterfaceOrientationPortrait;
    }
    

    文档说明:
    UIViewController对象属性supportedInterfaceOrientations,返回所支持的横竖屏的类型。

    声明

    @property(nonatomic, readonly) UIInterfaceOrientationMask supportedInterfaceOrientations;
    

    返回值

    返回当前ViewController需要被支持的方向。这个值是一个枚举类型UIInterfaceOrientationMask,它包含了ViewController支持的旋转方向。具体的取值如下:

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

    UIInterfaceOrientationMask简介:该方法在iOS8之后被添加的,会让你更得心应手的使用UITraitCollectionUITraitEnvironmentAPIs,在Size Class中也支持了这些属性,也无需再依据UIInterfaceOrientation来确定你的ViewController所支持的方向。
    在iOS8之前的版本中,你应该在supportedInterfaceOrientationsForWindow:方法中返回一个对应的值,来确定你ViewController的方向。这里不展开讲解。

    注意

    shouldAutorotate方法的返回值设为YES时,也就是当前控制器需要支持随设备旋转,那么当改变设备的方向时,系统会在rootViewController,也就是keyWindowrootViewController中调用supportedInterfaceOrientations方法,举个工程中常用的例子,如果当前工程的keyWindowrootViewControllerUITabBarController,那么系统会在该ViewController中调用supportedInterfaceOrientations
    在控制器中重写这个方法来返回需要支持的方向,在iPad上的默认值是UIInterfaceOrientationMaskAll,在iPhone上默认值是UIInterfaceOrientationMaskAllButUpsideDown。控制器在该方法中返回的方向值,实际上是取决于Info.plist文件或者appdelegateapplication:supportedInterfaceOrientationsForWindow:是否配置支持旋转。

    这里再说一下,支持屏幕旋转的info.plist文件和appdelegate中的方法。
    info.plist的配置实际上是在工程的target->general->Deployment Info 勾选Landscape LeftLandscape Right即可完成配置。
    如果在Appdelegate使用代码配置如下:

    //  如
    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
        // iPad上需要支持所有方向
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            return UIInterfaceOrientationMaskAll;
        } else { /* iphone */
        // iPhone上只支持竖屏
            return UIInterfaceOrientationMaskPortrait ;
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS屏幕旋转之Apple Document(苹果文档翻译)

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