iOS实现屏幕旋转有两种方式

1. 应用本身支持

2. 手动旋转UIView (这种方式我没找到旋转 系统控件的方法 如:键盘、导航、UIAlertView)

如果你只是要把竖屏的播放器,做成支持横屏的,没有其他界面操作, 就可以考虑用第二种方式去做,比较简单 ,不过要注意计算view Frame

这两种方式看你具体的使用场景了,具体场景选择合适的方式。

公司项目中有几个界面要支持横竖屏,(直播录制界面、直播观看界面、视频回看界面)。 刚开始我想着用第二种方式去解决,但是我们视频录制、观看界面有输入框,需要调用键盘,没找到可以手动旋转键盘的方式,就改为让应用本身支持屏幕旋转。具体做法如下:

我们项目有一个 UITabBarController ,它有3个子CustomNavigationController, 我写了一个

CustomNavigationController继承自 UINavigationController

1. tabBarController 中

- (BOOL)shouldAutorotate{

return [self.selectedViewController shouldAutorotate];

}

//支持的方向

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{

return [self.selectedViewController supportedInterfaceOrientations];

}

//初始方向

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{

return UIInterfaceOrientationPortrait;

}

2. CustomNavigationController 重写父类方法

- (BOOL)shouldAutorotate

{

return NO;

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

return UIInterfaceOrientationMaskPortrait;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

{

return UIInterfaceOrientationPortrait;

}

3. 我的支持屏幕旋转的3个ViewController是presentViewController出来的; 在支持旋转的界面加入以下代码:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{

return toInterfaceOrientation != UIDeviceOrientationPortraitUpsideDown;

}

- (BOOL)shouldAutorotate

{

if (可以在此处做判断,如果满足条件就可以旋转) {

return YES;

}

return NO;

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

if (可以在此处做判断,如果满足条件就支持竖屏) {

return UIInterfaceOrientationMaskPortrait;

}

return UIInterfaceOrientationMaskAllButUpsideDown;

}

还要做一件事---- 刷新UI布局;

4.

在ViewDidLoad中加入监听屏幕旋转的事件:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

在orientChange:方法中修改UI布局  ;

(注意: 看一下屏幕尺寸的变化 [[UIScreen mainScreen] bounds].size )

如果要控制CustomNavigationController push到的viewController的旋转,那么就在CustomNavigationController里面区分是哪个viewController,以便单独控制!

- (BOOL)shouldAutorotate

{

UIViewController *control = self.topViewController;

if ([control isKindOfClass:[@"支持旋转的ViewController" class]]) {

return [control shouldAutorotate];

}

return YES;

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

UIViewController *control = self.topViewController;

if ([control isKindOfClass:[@"支持旋转的ViewController" class]]) {

return [control supportedInterfaceOrientations];

}

return UIInterfaceOrientationMaskPortrait;

}

iOS实现屏幕旋转的更多相关文章

  1. ios实现屏幕旋转的方法

    1.屏蔽AppDelegate下面的屏幕旋转方法 #pragma mark - 屏幕旋转的 //- (UIInterfaceOrientationMask)application:(UIApplica ...

  2. 监听iOS检测屏幕旋转状态,不需开启屏幕旋转-b

    -(void)rotation_icon:(float)n { UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>] ...

  3. 监听iOS检测屏幕旋转状态,不需开启屏幕旋转

    -(void)rotation_icon:(float)n { UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>] ...

  4. iOS强制屏幕旋转

    /** 强制旋转屏幕为纵向 (注:这种方式 键盘不能旋转过来; iOS8.x下 UIAlterView旋转不过来  ) @return */ + (void)rotateOrientationPort ...

  5. 【iOS】屏幕旋转,屏幕自适应方向变化

    1. iOS有四个方向的旋转,为了保证自己的代码能够支持旋转,我们必须首先处理一个函数: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInter ...

  6. ios 关于屏幕旋转和屏幕晃动

    内置加速计是智能手机最酷的特性之一,ios可以通过这个小设备知道用户握持手机的方式,以及用户是否移动了手机,ios使用加速计处理自动旋转,并且许多游戏都是用它作为控制机制,它还可以用于检测摇动和其他突 ...

  7. iOS 控制屏幕旋转

    在你想支持横竖屏的viewController里面重写两个方法: 1 2 3 4 5 6 7 8 9 10 11 // 支持设备自动旋转 - (BOOL)shouldAutorotate {      ...

  8. IOS7学习之路八(iOS 禁止屏幕旋转的方法)

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { retu ...

  9. OpenGL ES 响应屏幕旋转 iOS

    iOS下使用OpenGL 如果使用GLKit View 那么不用担心屏幕旋转的问题,说明如下: If you change the size, scale factor, or drawable pr ...

随机推荐

  1. 虚幻4外包团队-推荐非常全面的的Unreal教程

    <Unreal SDK 游戏开发从入门到精通(UnrealScript语法.UI Scene界面.UDK独立开发游戏)> 课程讲师:Shark  课程分类:.net 适合人群:初级 课时数 ...

  2. setTimeout()与setInterval()

    一.setTimeout与setInterval的用法(http://www.css88.com/archives/5804) setTimeout是超时调用,javascript是一个单线程的解析器 ...

  3. Debian MySQL 卸载和安装 PHP安装

    如果是apt-get安装的 sudo apt-get --purge remove mysql-serversudo apt-get --purge remove mysql-clientsudo a ...

  4. 【maven】之配置开发,测试,正式环境pom.xml文件

    在进行web程序开发,如果项目组没有使用自动化发布工具(jenkins + maven + svn + tomcat ),我们一般会使用maven的热部署来完成发布,在部署的过程中我们开发,测试,生产 ...

  5. es6 const

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. sqlite升级--浅谈Android数据库版本升级及数据的迁移

    Android开发涉及到的数据库采用的是轻量级的SQLite3,而在实际开发中,在存储一些简单的数据,使用SharedPreferences就足够了,只有在存储数据结构稍微复杂的时候,才会使用数据库来 ...

  7. 开发安卓应用之中兴手机与macbook pro 连接设定

    目标: 把中兴手机和macbook pro 连接在一起,实现真机调试安卓应用. 工具: 手机型号:zte v956 mac os: OS X 10 Eclipse: Android Developer ...

  8. 为了让你的网页能在更多的服务器上正常地显示,还是加上“SET NAMES UTF8”吧

    Repinted:http://blog.csdn.net/class1/archive/2006/12/30/1469298.aspx 为了让你的网页能在更多的服务器上正常地显示,还是加上“SET ...

  9. Linux disk_partition_dev_马士兵_note

    一般装Linux会遇到的问题: 找不到硬件驱动 现在主流的一些硬件 不支持Linux驱动   尽量找主流的硬件,尽量找老一点的硬件   装系统: 1.记下 系统 ---->到时候要找驱动   2 ...

  10. apache配置Allow详解及25个常见问题

    http://www.cnblogs.com/top5/archive/2009/09/22/1571709.html apache常见25个问题:http://blog.csdn.net/keda8 ...