1、屏蔽AppDelegate下面的屏幕旋转方法

#pragma mark - 屏幕旋转的
//- (UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
//{
// return UIInterfaceOrientationMaskPortrait;
//}

2、对UINavigationController和UITabBarController写两个扩展类别,此东西不需要在具体的ViewController中引用

UINavigationController+Autorotate.h

//
// UINavigationController+Autorotate.h
// fitmiss
//
// Created by bill on 15/12/16.
// Copyright © 2015年 lear. All rights reserved.
// #import <UIKit/UIKit.h> @interface UINavigationController (Autorotate) @end

UINavigationController+Autorotate.m

//
// UINavigationController+Autorotate.m
// fitmiss
//
// Created by bill on 15/12/16.
// Copyright © 2015年 lear. All rights reserved.
// #import "UINavigationController+Autorotate.h" @implementation UINavigationController (Autorotate) - (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
} - (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
} - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
} @end

UITabBarController+Autorotate.h

//
// UITabBarController+Autorotate.h
// fitmiss
//
// Created by bill on 15/12/16.
// Copyright © 2015年 lear. All rights reserved.
// #import <UIKit/UIKit.h> @interface UITabBarController (Autorotate) @end

UITabBarController+Autorotate.m

//
// UITabBarController+Autorotate.m
// fitmiss
//
// Created by bill on 15/12/16.
// Copyright © 2015年 lear. All rights reserved.
// #import "UITabBarController+Autorotate.h" @implementation UITabBarController (Autorotate) - (BOOL)shouldAutorotate
{
return [self.selectedViewController shouldAutorotate];
} - (NSUInteger)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
} - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
} @end

3.在使用的ViewController中的再次重写这三个方法,可以在根ViewController中重写如下方法,就能实现竖屏,子ViewController再重写实现旋转

- (BOOL)shouldAutorotate{
//是否允许转屏
return NO;
} - (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
//viewController所支持的全部旋转方向
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
} - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
//viewController初始显示的方向
return UIInterfaceOrientationPortrait;
}

注意:在使用的时候发现在ios9以下的版本出现一个奇怪的问题,就是编译出来的app默认是横的,解决办法是看app.plist下面Supported interface orientations里的列表中,正屏的是不是排在第一个。

摘自网上网友的回复内容,如下:

以下方法仅对deploy target大于等于iOS6的工程有效,如果题主的应用需要支持iOS5(默哀),请pass。

  • 在info.plist中设置方向,包含你需要的所有方向,以题中意,UpSideDown和LandScapeLeft;
  • 继承UITabBarController,override以下三个方法
 - (BOOL)shouldAutorotate
{
return [self.selectedViewController shouldAutorotate];
} - (NSUInteger)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
} - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
  • 继承UINavigationController,override和UITabBarController中相同的方法,将selectedViewController改为topViewController
 - (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
} - (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
} - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
  • 在真正实现界面的ViewController里,override上面这三个方法,override规则如下:
    preferredInterfaceOrientationForPresentation表示viewController初始显示时的方向;
    supportedInterfaceOrientations是在该viewController中支持的所有方向;
    shouldAutorotate表示是否允许旋屏。

流程说明
首先,对于任意一个viewController,iOS会以info.plist中的设置和当前viewController的preferredInterfaceOrientationForPresentation和supportedInterfaceOrientations三者支持的方法做一个交运算,若交集不为空,则以preferredInterfaceOrientationForPresentation为初始方向,交集中的所有方向均支持,但仅在shouldAutorotate返回YES时,允许从初始方向旋转至其他方向。若交集为空,进入viewController时即crash,错误信息中会提示交集为空。
其次,UINavigationController稍有些特别,难以用常规API做到同一个naviVC中的ViewController在不同方向间自如地切换。(如果去SO之类的地方搜索,会找到一个present empty viewController and then dismiss it之类的hacky trick,不太建议使用),如果要在横竖屏间切换,建议使用presentXXX方法。
再次,AppDelegate中有一个委托方法可以动态的设置应用支持的旋转方向,且此委托的返回值会覆盖info.plist中的固定设置。使用该方法的便利之处不言自明,但缺点是搞明白当前哪个ViewController即将要被显示,很可能会导致耦合增加;
最后,以上均为个人在iOS8 SDK下得到的实践结果,请题主结合工程实际参考使用。

ios实现屏幕旋转的方法的更多相关文章

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

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

  2. iOS实现屏幕旋转

    iOS实现屏幕旋转有两种方式 1. 应用本身支持 2. 手动旋转UIView (这种方式我没找到旋转 系统控件的方法 如:键盘.导航.UIAlertView) 如果你只是要把竖屏的播放器,做成支持横屏 ...

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

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

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

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

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

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

  6. iOS强制屏幕旋转

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

  7. iOS 控制屏幕旋转

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

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

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

  9. Android应用不随手机屏幕旋转的方法

    在主配置文件里面.在需要设置的activity项后面加上 android:screenOrientation="portrait",这个activity就保持竖屏显示了:在每个ac ...

随机推荐

  1. PHPMySQL 中 pdo文件的增删改查

    <?php //PDO类的增删改查 try { $pdo = new PDO("mysql:host=localhost;dbname=ooo","root&quo ...

  2. Java类文件最大限制

    今天在往一个jsp文件里添加代码时,项目跑起来访问这个jsp时报错.. The code of method _jspService(HttpServletRequest, HttpServletRe ...

  3. createjs 利用createjs 写拼图功能

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  4. application loader上传报90158错误

    ERROR ITMS-90158:"The following URL schemes found in your app are not in the current format:[XX ...

  5. 通过MD5排除重复文件

    今天下载了好多美女图片壁纸,可是看了一下发现好多图片重复了,有强迫症的我必须把重复的都给剔除掉,首先想到的当然是百度了,问问度娘有没有什么图片去重的工具,搜了一下还真有.奈何本人智商捉急用不来这高级的 ...

  6. 计算机网络(2)-----ARP协议

    ARP协议(Address Resolution Protocol) 概念 地址解析协议,即ARP(Address Resolution Protocol),是根据IP地址获取物理地址的一个TCP/I ...

  7. LazyLoad使用注意

    今天使用ProgressHUD,进行网络请求后显示加载完成提示框,但是无效,检查以后发现数据源数组使用了懒加载,在调用数组之前调用ProgressHUD里的方法,根本无效啊!以后用懒加载注意.

  8. github删除带有文件的文件夹

    1. git pull you git url2. git checkout 3. rm -rf dirName4. git add --all5. git commit -m"remove ...

  9. python3登录极路由并读取宽带帐号帐号密码.py

    python3登录极路由并读取宽带帐号帐号密码,fiddler抓包分析过程略... 步骤:1.登录路由,提取stok. 2.用stok拼成url,post请求 3.解析json数据 代码: " ...

  10. int a

    系统编译之后所有的变量都存储到符号表中,并且每个表项被分配一个符号ID,一般也是数字的,可以根据该符号的ID直接访问符号的值内存中的数据都是二进制的,没有ASCII值ASCII只在为便于人的理解,对二 ...