iOS6以后的单个控制器横竖屏显示以及旋转屏控制技巧,附带iOS8以后显示电池状态栏
一、在应用中从竖屏模式强制转换为横屏模式
第一种方法:通过模态弹出视图的方式,使得特定ViewController坚持特定的interfaceOrientation
(1)iOS6之后提供了这样一个方法,可以让你的Controller倔强的坚持某个特定的interfaceOrientation:- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0)
{
return UIInterfaceOrientationLandscapeRight;//左下右上显示 这里有5种供选择,具体什么方向显示,自己可以试一下
//UIInterfaceOrientationUnknown
//UIInterfaceOrientationPortrait
//UIInterfaceOrientationPortraitUpsideDown
//UIInterfaceOrientationLandscapeLeft
//UIInterfaceOrientationLandscapeRight
}
(2)当然,使用这个方法是有前提的,就是当前ViewController是通过全屏的Presentation(模态视图)方式展现出来的。而且需要设置下面方法返回值为NO,这样控制器就不会再进行旋转,而是以你设定方向显示。
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0)
{
return NO;
}
第二种方法:通过人为的办法改变view.transform的属性。
具体办法:view.transform一般是View的旋转,拉伸移动等属性,类似view.layer.transform,区别在于View.transform是二维的,也就是使用仿射的办法通常就是带有前缀CGAffineTransform的类(可以到API文档里面搜索这个前缀的所有类),而view.layer.transform可以在3D模式下面的变化,通常使用的都是前缀为CATransform3D的类。
这里要记住一点,当你改变过一个view.transform属性或者view.layer.transform的时候需要恢复默认状态的话,记得先把他们重置可以使用view.transform = CGAffineTransformIdentity,或者view.layer.transform = CATransform3DIdentity,假设你一直不断的改变一个view.transform的属性,而每次改变之前没有重置的话,你会发现后来的改变和你想要的发生变化了,不是你真正想要的结果。
好了,上面介绍了旋转的属性,接下来就是关键了。官方提供了一个办法就是查看当前电池条的状态UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;通过这个办法,你可以知道当前屏幕的电池条的显示方向,而且你还可以强制设置他的显示方向,通过设置这个属性就OK了,可以选择是否动画改变电池条方向。有了这两个那我们就可以任意的改变我们想要的显示方式了。
(1)获取当前电池条的方向UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation (2)获取当前屏幕的大小CGRect frame = [UIScreen mainScreen].applicationFrame; (3)设置我们的View的中心点 CGPoint center = CGPointMake(frame.origin.x + ceil(frame.size.width/2), frame.origin.y + ceil(frame.size.height/2)); (4)根据当前电池条的方向,获取需要旋转的角度的大小。通常 - (CGAffineTransform)getARotation {
if (orientation == UIInterfaceOrientationLandscapeLeft) {
return CGAffineTransformMakeRotation(M_PI*1.5);
} else if (orientation == UIInterfaceOrientationLandscapeRight) {
return CGAffineTransformMakeRotation(M_PI/2);
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
return CGAffineTransformMakeRotation(-M_PI);
} else {
return CGAffineTransformIdentity;
}
} (5)可以动画的改变我们view的显示方式了
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:YES]; CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;(获取当前电池条动画改变的时间)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration]; //在这里设置view.transform需要匹配的旋转角度的大小就可以了。
//把(4)中返回的rotation赋给self.view.transform
self.view.transform = [self getARotation]; [UIView commitAnimations];
第三种方法:通过setOrientation:的办法强制性的旋转到一个特定的方向。
注意:Apple在3.0以后都不支持这个办法了,这个办法已经成为了私有的了,但是要跳过App Stroe的审核,需要一点巧妙的办法。
不要直接调用[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationLandscapeRight]这样的办法来强制性的横屏,这样导致你的程序是很难通过App Store审核的。但是你可以选择使用performSelector的办法来调用它。具体就几行代码如下: //强制横屏
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
[[UIDevice currentDevice] performSelector:@selector(setOrientation:)
withObject:(id)UIInterfaceOrientationLandscapeRight];
}
总结:如果第一种办法可以满足你需要的话,最好使用第一种办法,因为第一种方法在App Store肯定没有问题,而且也比较简单;第二种方法在App Store也是没问题的,但是稍微复杂一些;第三种的话是需要冒风险的,但是如果你的结构太复杂了,导致使用前两种办法人为很难控制的话,可以尝试简单的使用第三种办法。
二、屏幕自适应重力感应进行旋转,实现对每个viewController的单独控制:
子类化UINavigationController,增加方法
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
} - (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
并且设定其为程序入口,或指定为 self.window.rootViewController
随后添加自己的view controller,如果想禁止某个view controller的旋屏:(支持全部版本的控制)- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
} -(BOOL)shouldAutorotate
{
return NO;
} -(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
如果想又开启某个view controller的全部方向旋屏支持:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} -(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
} -(BOOL)shouldAutorotate
{
return YES;
}
从而实现了对每个view controller的单独控制。
顺便提一下,如果整个应用所有view controller都不支持旋屏,那么干脆:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait;
}
横屏时,ios7上有电池状态栏,在iOS8就没有电池状态栏了
上次开发过程遇到有个页面横屏显示,当设置横屏展示时,就没有电池状态栏了。查了一些资料才发现,原来
横屏时,ios7上有电池状态栏,在iOS8就没有电池状态栏了,是因为iOS8默认横屏时将电池状态栏隐藏了,这是iOS8的新特性。
下面说说怎么让状态栏在横屏时显示出来:
在plist文件中将 View controller-based status bar appearance 设置为 NO

在appdelegate.m中的- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary)launchOptions 方法中添加下面代码即可。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
return YES;
}
iOS6以后的单个控制器横竖屏显示以及旋转屏控制技巧,附带iOS8以后显示电池状态栏的更多相关文章
- [置顶] Android系统移植与调试之------->如何修改Android设备状态条上音量加减键在横竖屏的时候的切换与显示
这两天由于一个客户的要求,将MID竖屏时候的状态条上的音量键去掉.所以尝试修改了一下,成功了,分享一下经验. 先看一下修改后的效果图,如下所示 . 横屏的时候:有音量加减键 竖屏的时候:音量加减键被去 ...
- Android系统移植与调试之------->如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
这两天由于一个客户的要求,将MID竖屏时候的状态条上的音量键去掉.所以尝试修改了一下,成功了,分享一下经验. 先看一下修改后的效果图,如下所示 . 横屏的时候:有音量加减键 竖屏的时候:音量加减键被去 ...
- android横竖屏控制
代码中设置activity屏幕为全屏,并设置横竖屏状态 getwindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowM ...
- ios6和ios5横竖屏切换
记录于2013/8/5 在切换横竖屏的时候调用到的一些委托方法: #pragma mark - UIApplicationDelegate //写在Appdelegate中,在具体的某一视图控制器 ...
- ios5和ios6横竖屏支持及ipad和iphone设备的判断
ios5和ios6横竖屏支持及ipad和iphone设备的判断 判断是ipad还是iphone设备.此定义在PayViewControllerDemo-Prefix.pch 定义如下: #define ...
- iOS 之 设置横竖屏及ViewWillAppear及视图与导航控制器嵌套时的不同反应
设置横竖屏不成功,可能跟所在导航控制器有关 视图没有导航控制器时,又有可能不调用ViewWillAppear,这是我遇到的问题.
- 设置Android应用程序横竖屏显示
设置Android应用程序横竖屏显示有2中方式: 1.在mainfest中增加android:screenOrientation属性 <?xml version="1.0" ...
- object-c中实现特定一个或者多个页面横竖屏,其他界面保持竖屏显示。
1.首先设置项目支持的屏幕方向.info.plist设置(自行设置) 2.写一个子类CusNavigationController 继承 UINavigationController,在CusNavi ...
- 控制器全屏显示.不展示导航栏navigationbar
有些时候需要 让控制器全屏显示 ,不需要导航栏,或者说是在导航栏底部 基本情况: >控制器全屏 ``` if (@available(iOS 11.0, *)) { self.tableView ...
随机推荐
- lc面试准备:Implement Queue using Stacks
1 题目 Implement the following operations of a queue using stacks. push(x) -- Push element x to the ba ...
- Hadoop上结合opencv\javacv
mac上安装opencv 1. 去 http://opencv.org 下载最新版OpenCV for Linux/Mac源文件,目前版本是2.4.3.下载后解压.2. 去 http://www.cm ...
- 14.6.3.2 Configuring Multiple Buffer Pool Instances 配置多个Buffer Poll 实例:
14.6.3.2 Configuring Multiple Buffer Pool Instances 配置多个Buffer Poll 实例: 对于系统有多个buffer pools 在多个字节范围, ...
- centos 6.5安装esx sdk
1. RHEL 6.3 64 bit yum install e2fsprogs-devel libuuid-devel yum install glibc.i686 yum install perl ...
- BZOJ2134: 单选错位
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2134 题解:因为每个答案之间是互不影响的,所以我们可以挨个计算. 假设当前在做 i 题目,如果 ...
- (转载)获取当前运行的PHP版本信息
(转载)http://www.clovery.org/get-the-php-version-information.html 获取PHP运行环境信息,可以使用下面的函数. <?php phpi ...
- 机器安装第二个tomcat ,出现报错如何解决
1.本机安装第二个 tomcat 后,出现 报错如下图所示 最后解决办法 是 在安装的时候 ,windows 服务名称 和 另一个tomcat 起不一样的 名称就可以了 如下图
- ZOJ Problem Set - 3758 素数
Singles' Day Time Limit: 2 Seconds Memory Limit: 65536 KB Singles' Day(or One's Day), an unofficial ...
- Java程序员必知的8大排序算法
8种排序之间的关系 直接插入排序 (1)基本思想:在要排序的一组数中,假设前面(n-1)[n>=2] 个数已经是排 好顺序的,现在要把第n个数插到前面的有序数中,使得这n个数 也是排好顺序的.如 ...
- php判断是不是ajax访问
<?php // php 判断是否为 ajax 请求 if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strto ...