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 ...
随机推荐
- LeetCode解题报告:Reorder List
Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… Yo ...
- java HastTable实现
public class HashTable{ private String[] name; //关键字 private int sum; //容量 public static void ...
- IEEE 802
IEEE 802又称为LMSC(LAN /MAN Standards Committee, 局域网/城域网标准委员会), 致力于研究局域网和城域网的物理层和MAC层中定义的服务和协议, 对应OSI网络 ...
- Json 的日期格式转换成DateTime
JSON 的日期形式:”/Date(1242357713797+0800)/” , 下面我们就用以下C#的方法将他转换成DateTime类型: /// <summary> /// Json ...
- Linux学习笔记23——取消线程
一 相关函数 1 发送终止信号 #include <pthread.h> int pthread_cancel(pthread_t thread); 2 设置取消状态 #include & ...
- 实现自己的脚本语言ngscript之一:词法分析
正则表达式的理论基础可以参考装配脑袋的 这个 自己动手开发编译器(二)正则语言和正则表达式 这个 自己动手开发编译器(三)有穷自动机 还有这个 自己动手开发编译器(四)利用DFA转换表建立扫描器 如果 ...
- Django中的Ajax
Ajax 很多时候,我们在网页上请求操作时,不需要刷新页面.实现这种功能的技术就要Ajax!(本人定义,不可迷信) jQuery中的ajax就可以实现不刷新页面就能向后台请求或提交数据的功能,我们仍然 ...
- myeclipse svn
打开myeclipse的help---install from site 点击add弹出对话框 在输入框中输入对应内容 http://subclipse.tigris.org/update_1.10. ...
- Python - 字典(dict) 详解 及 代码
字典(dict) 详解 及 代码 本文地址: http://blog.csdn.net/caroline_wendy/article/details/17291329 字典(dict)是表示映射的数据 ...
- 【设计模式 - 6】之桥接模式(Bridge)
1 模式简介 举个例子,人.车和公路是三个维度,人开着车在公路上行驶,就是将这三个维度进行了关联.人分男人(Man)和女人(Woman),车分小轿车(Car)和公共汽车(Bus),公路分市区 ...