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. CSS3判断手机横屏竖屏

    原理: 当用户旋转屏幕的时候,会进入到你的监听方法中,然后通过window.orientation来获取当前屏幕的状态:0 - 竖屏90 - 逆时针旋转横屏-90 - 顺时针旋转横屏180 - 竖屏, ...

  2. iOS开发多线程篇—GCD介绍

    iOS开发多线程篇—GCD介绍 一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 G ...

  3. bzoj 3130: [Sdoi2013]费用流

    #include<cstdio> #include<iostream> #define M 10000 #define inf 0x7fffffff #include<c ...

  4. WCF 断点不会命中

    VS的调试模式改为Debug 工具—选项—调试—常规中的‘要求源文件和原始版本完全匹配’的勾去掉

  5. iOS学习之六种传值方式

    iOS页面传值方式 应用于: 两个互动的界面:1)页面一跳转到页面二,页面一的textField的值传给页面二的label.2)A页面跳转到B页面,B页面再跳转回A页面(注册页面跟登录页面) 两个不互 ...

  6. OpenResty 安装及使用(第一篇安装)

    OpenResty搭建 1.openResty介绍 OpenResty (也称为 ngx_openresty)是一个全功能的 Web 应用服务器.它打包了标准的 Nginx 核心,很多的常用的第三方模 ...

  7. Eclipse设置JSP页面的默认编码

    1.一般新建jsp页面是默认编码为ISO-8895-1编码.但是,实际应用中为避免编码问题带来的麻烦,我们一般需要设置默认编码为UTF-8. 2.设置 Eclipse->Window->P ...

  8. 三部曲一(数据结构)-1022-Gold Balanced Lineup

    Gold Balanced Lineup Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Othe ...

  9. C/C++ 网络库介绍

    C/C++ 网络库介绍 Aggregated List of Libraries(Source Link) Boost.Asio is really good. Asio is also availa ...

  10. (实用篇)php数组查找函数in_array()、array_search()、array_key_exists()使用

    php在数组中查找指定值是否存在的方法有很多,记得很久以前我一直都是傻傻的用foreach循环来查找的,下面我主要分享一下用php内置的三个数组函数来查找指定值是否存在于数组中,这三个数组分别是 in ...