1 配置
//appDelegate,默认是plist里面UIInterfaceOrientation决定(Build Settings->General设置)。
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
//优先级高于info.plist
}
系统没有关闭自动旋转时
//UIViewController.m
#pragma mark - rotation
//定义是否支持自动旋转
-(BOOL)shouldAutorotate {
return YES;
}
//定义支持旋转的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
return isPad ? UIInterfaceOrientationMaskLandscape : UIInterfaceOrientationMaskPortrait;
}
//定义第一次进来的时候的方向
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return isPad ? UIInterfaceOrientationLandscapeLeft : UIInterfaceOrientationPortrait;
}
2 生命周期
//controller
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
if (size.width > size.height) {
//横屏设置,为防止遮挡键盘,调整输入视图的高度
self.textView_height.constant = 50;
}else{
//竖屏设置
self.textView_height.constant = 200;
}
}
//子视图
- (void)layoutSubviews {
[super layoutSubviews];
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationMaskPortrait) {
//竖屏布局
} else {
//横屏布局
}
}
3 设备方向&屏显方向
UIDeviceOrientation
机器硬件方向,只读,可以通过KVC设置
[UIDevice currentDevice].orientation
UIInterfaceOrientation
程序界面方向,属性只读,有set方法可写。
[UIApplication sharedApplication].statusBarOrientation
注意
- 注意弹框View与旋屏
- 横竖屏分辨率数
//iOS 8之后
// 竖屏:
UIScreen.mainScreen().bounds: (0.0,0.0,320.0,568.0)
// 横屏:
UIScreen.mainScreen().bounds: (0.0,0.0,568.0,320.0)