完美解决 iOS 中只旋转自己想要旋转的屏幕
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 中只旋转自己想要旋转的屏幕相关推荐
- htmltd文本自动换行,完美解决table中td里面的内容自动换行
完美解决table中td里面的内容自动换行2018-08-03 11:50 对于将td里面的内容自动换行,在很久以前就遇到的了,但是一直没有完美的解决. 今天要打印一个报表,有一列中的内容太长,将ta ...
- 上传图片方向不对 php,如何解决IOS中html5上传图片方向问题?
这篇文章主要介绍了IOS中html5上传图片方向问题解决方法的相关资料,需要的朋友可以参考下 用html5编写图片裁切上传,在iphone手机上可能会遇到图片方向错误问题,在此把解决方法和大家分享一下 ...
- winform界面嵌入dwg图纸_完美解决窗体中预览DWG图形(C#版)
看到完美解决VB.NET窗体中预览DWG图形帖子后,用C#代码 实现如下: class ViewDWG { struct BITMAPFILEHEADER { public short bfType; ...
- 微信小程序中解决iOS中new Date() 时间格式不兼容
本周写小程序,遇到的一个bug,在chrome上显示得好好的时间,一到Safari/iPhone 就报错 "invalid date",时间格式为"2019.06.06 ...
- 完美解决Visio中MathType公式变形的问题以及visio图转pdf之后公式不显示的问题
很多博主说的转换为打印是可以解决公式变形的问题,但是放大后会有些地方模糊.然后我发现啊!!! 插入公式前先插入矩形框,把公式和矩形框组合起来,就可以了,完美解决,不会糊,也不会变形!!!Nice
- HTML5 完美解决javascript中iphone手机和android手机复制文本到剪切板问题
1. 执行以下解决方案条件:(这个是原理) ①执行复制方法时 所复制文字不能被任何 块级元素和行内块元素和行内元素遮盖否则无效:(解决方案:将文本通过绝对定位或其他方式移除屏幕外) ②ios中不能复制 ...
- 解决Unity3D中多层级结构子物体旋转受父物体尺寸影响的问题
前言 在使用unity3D进行场景设计和物体控制的过程中,多层物体嵌套是经常会用到的一个结构.多层级物体中,每个层次的物体可能都会有子物体,这就导致了子物体的一些行为会收到父物体参数的影响.本文讨论子 ...
- 完美解决html中select的option不能隐藏的问题。
开发过程中无意遇到这个问题,解决问题第一时间就是百度.结果得到如下方法: 1.首先设置option的display:none的方案肯定是不可行了: 2.某网友提出的两种方案: a.在option标签上 ...
- 完美解决IDEA 中Maven插件报红详细攻略(含阿里云镜像下载失败),差点泪崩...冲冲冲
阿里云镜像: <!-- 阿里云仓库 --> <mirror><id>nexus-aliyun</id><mirrorOf>central&l ...
最新文章
- 【号外】来人鸭~ 本公众号纳新啦~
- GCC选项_-Wl,-soname
- 入职阿里5年,他如何破解“技术债”?
- 网络中间设备路在何方
- oracle 中表,oracle中表操作
- Bifrost微前端框架及其在美团闪购中的实践
- OpenShift 4 之使用https协议访问Route
- 添加Maven(mvn)、sbt的国内仓库
- [转] 面向对象编程介绍
- 8255工作方式2——双向选通输入输出(A口)
- 选择SEO服务时要注意的问题
- jdbcTemplate测试报错:没有合适的驱动
- tcp 抓包出现spurious retransmission
- GPIO输入输出模式原理(八种工作方式附电路图详解)
- 【为人处事】:如何识人
- 手机发热是什么原因?
- android布局靠底部,android – 使用layout_gravity =“bottom”放置在LinearLayout的底部
- Python怎么识别文字?正确的方法详解
- IMDb Large Movie Review-数据集
- c#文件操作代码段保存