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下得到的实践结果,请题主结合工程实际参考使用。

最新文章

  1. Android中点击事件的实现方式
  2. Neural Network学习(二)Universal approximator :前向神经网络
  3. spring多数据源的处理 mybatis实现跨库查询
  4. Android的Activity屏幕切换动画-左右滑动切换
  5. BEvent_标准控件Event的用法(案例)(待整理)
  6. PC上面的蓝牙的通信(C#)
  7. c++学习_1
  8. mysql_convert_table_format 批量修改表引擎
  9. 使用astyle格式化代码
  10. Grunt Server:Fatal error: Port 35729 is already in use by another process.
  11. Android启动Activity
  12. C# 说说lock到底锁谁?(1)
  13. R语言 重命名目录下所有文件
  14. UVALive - 4222
  15. C# 重启程序本身
  16. HTML5-CSS3-JavaScript(1)
  17. centos下安装&amp;&amp;配置redis
  18. [JavaScript模块演化简史]摘要
  19. [svc]linux文件权限
  20. jackson 转换 enum 类型

热门文章

  1. C#日常知识
  2. 运用js解决java selenium元素定位问题
  3. mybaits入门
  4. 几款值得推荐的android(安卓)开源框架简介
  5. Javascript中封装window.open的例子
  6. hdu 2065
  7. BZOJ 2763 分层图最短路
  8. 18、SQL提高篇(变量的使用 拓展)
  9. DIV+CSS制作二级横向弹出菜单,略简单
  10. PAT (Basic Level) Practise:1031. 查验身份证