部分转自:http://blog.youkuaiyun.com/ioswyl88219/article/details/24711197
系统支持横屏顺序
默认读取plist里面设置的方向(优先级最高)等同于Xcode Geneal设置里面勾选
application window设置的级别次之
然后是UINavigationcontroller
级别最低的是viewcontroller
以下三条式子为控制屏幕翻转的核心代码
- (BOOL)shouldAutorotate//是否支持旋转屏幕{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations//支持哪些方向{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation//默认显示的方向{
return UIInterfaceOrientationPortrait;
}
只要在plist或者General设置了单一方向,在工程里面的任何代码都会失效。所以想要旋转屏幕,必须让你的工程支持所旋转的方向。(但是这里还有一种方式是把当前控制器的View或者view.layer做transform旋转,但是我认为这并不是真正的旋转屏幕,一点可以证明的就是:状态条并不随着旋转)
其实这个优先级跟你的window的rootViewcontroller有关系,如果rootViewcontroller是UITabbarViewontroller,那么tabbar就类似前面所说的UINavigationcontroller,在iOS6之后好多方法发生了改变,之前的一个屏幕旋转的方法就失效了,更改位上面的方法。
其实屏幕旋转在手机里面有设置,上拉菜单就会有关闭屏幕旋转的方式,但是大家都知道好多应用在关闭屏幕旋转的时候也可以进行屏幕旋转---通过某一个按钮的事件来触发,这就是强制旋转(前提依然是你的工程设置支持你所要旋转的方向)
屏幕设置为
在info.plist文件中将 View controller-based status bar appearance
设置为NO
在application:didFinishLaunchingWithOptions:
中添加下面代码
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
如此之后你就会发现消失的状态条有乖乖的回来了。
设置部分屏幕旋转:
根视图控制器为NavigationController
创建继承NAV的类
- (BOOL)shouldAutorotate {
//允许选择的视图控制器
if ([NSStringFromClass([self.visibleViewController class]) isEqualToString:@"CCViewController"]) {
return YES;
}
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
根视图控制器为TabBarController
创建NavigationController和TabBarVontroller的category
这里注意不能在tabbarcontroller的类里写旋转方法 否则以下方法会无效
#import UITabBarController+autoRotate.h
#import <UIKit/UIKit.h>
@interface UITabBarController (autoRotate)
-(BOOL)shouldAutorotate;
-(NSUInteger)supportedInterfaceOrientations;
@end
#import "UITabBarController+autoRotate.h"
@implementation UITabBarController (autoRotate)
-(BOOL)shouldAutorotate{
return [self.selectedViewController shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations{
return [self.selectedViewController supportedInterfaceOrientations];
}
@end
@interface UINavigationController (autoRotate)
-(BOOL)shouldAutorotate;
-(NSUInteger)supportedInterfaceOrientations;
@end
在这里写上需要部分允许旋转的视图控制器
-(BOOL)shouldAutorotate{
if ([NSStringFromClass([self.visibleViewController class])isEqualToString:@"PicShowViewController"]) {
return YES;
}
// return [self.visibleViewController shouldAutorotate];
return NO;
}
-(NSUInteger)supportedInterfaceOrientations{
return [self.visibleViewController supportedInterfaceOrientations];
}
监听设备旋转
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onDeviceOrientationChange) name:UIDeviceOrientationDidChangeNotification object:nil];
-(void)onDeviceOrientationChange
{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(orientation)) {
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
}else if (orientation==UIDeviceOrientationPortrait){
[self.collectionView reloadData];
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
}
}