美文网首页
iOS的屏幕旋转学习笔记(orientation)

iOS的屏幕旋转学习笔记(orientation)

作者: Jin丶hy | 来源:发表于2016-08-22 16:23 被阅读32次

由于项目的需要,在播放视频的ViewController中需要支持横屏旋转播放器,故重新写了一下整个app的控制。

开始吧,首先保证一下自己的项目是支持三个方向的旋转的。

在iPad中,苹果默认支持四个方向的旋转;在iPhone中,苹果默认只支持3中方向(建议按照套路来)

这里明确一下,相信大家做项目的时候都已经有一种意识,就是在开始项目的时候,都配备了 BaseViewController、BaseNavigationController 这两个最基础的类以供所有的ViewController,NavigationController继承。

额:如果没有,建议尽快添加 :-D

只要项目支持三个方向的旋转,不添加任何代码的情况下,app在手机旋转的时候就会自动旋转的。

在BaseNavigationController增加下面两个方法:


- (BOOL)shouldAutorotate {

    if (self.topViewController != nil) {
        //如果当前有VC,获取VC里面的shouldAutorotate
        return [self.topViewController shouldAutorotate];

    }

    return [super shouldAutorotate];

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {

    if (self.topViewController != nil) {
        //如果当前有VC,获取VC里面的supportedInterfaceOrientations
        return [self.topViewController supportedInterfaceOrientations]; 

    }

    return [super supportedInterfaceOrientations];

}

在BaseViewController增加下面两个方法:

- (BOOL)shouldAutorotate {
    //这里需要返回YES 
    // 如果你的项目仅仅在某个VC里面支持横屏,其他都不支持的话,这里也需要返回YES
    return YES;      
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    //如果你的项目仅仅在某个VC里面支持横屏,其他都不支持的话,这里也需要返回UIInterfaceOrientationMaskPortrait
    //而如果你的项目每一个页面都需要支持横屏,这里返回UIInterfaceOrientationMaskAllButUpsideDown就可以了
    return UIInterfaceOrientationMaskPortrait;
}

现在的代码代表是项目所有页面都只支持一个方向Portrait,不支持横屏。
如果你现在新建一个需要横屏的LandscapeViewController,则你只需要在 LandscapeViewController.h 继承BaseViewController

@interface LandscapeViewController : BaseViewController

在LandscapeViewController.m 里面 增加

#pragma mark 开放横屏切换
- (BOOL)shouldAutorotate {
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

现在就实现了在整个项目中 仅仅LandscapeViewController这个VC能旋转。

相关文章

  • iOS的屏幕旋转学习笔记(orientation)

    由于项目的需要,在播放视频的ViewController中需要支持横屏旋转播放器,故重新写了一下整个app的控制。...

  • iOS Rotation

    iOS屏幕旋转学习笔记iOS开发中使用屏幕旋转功能的相关方法 1、基本知识点解读 了解屏幕旋转首先需要区分两种 o...

  • 关于iOS屏幕旋转

    在iOS开发中,最常见的屏幕旋转方案: 取消Device Orientation的选择按钮取消选中 在appDel...

  • iOS开发:屏幕的旋转

    Device Orientation:设备方向 UIInterfaceOrientation:界面方向 屏幕旋转的...

  • 旋转屏幕的流程

    基础知识: 了解屏幕旋转首先需要区分两种 orientation device orientation 设备的物理...

  • iOS 16.0 适配记录

    1、屏幕旋转问题:[Orientation] BUG IN CLIENT OF UIKIT: Setting UI...

  • iOS屏幕方向控制

    iOS屏幕旋转学习笔记 - CocoaChina 苹果开发中文站 - 最热的iPhone开发社区 最热的苹果开发...

  • 屏幕旋转

    屏幕旋转 推荐文档 了解UIWindow——UIWindow实践 iOS屏幕旋转问题总结 IOS:屏幕旋转与变换 ...

  • React native orientation 屏幕旋转监听

    android 和 iOS 都有在全局设置中固定屏幕方向的办法,但这样不是特别理想,因为项目中,在不同页面可能会需...

  • iOS学习笔记03--屏幕旋转

    前提:你需要把controller.view作为window的subview,也即是需要设置window的root...

网友评论

      本文标题:iOS的屏幕旋转学习笔记(orientation)

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