1 配置
//appDelegate供常,默認(rèn)是plist里面UIInterfaceOrientation決定(Build Settings->General設(shè)置)。
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
//優(yōu)先級高于info.plist
}
系統(tǒng)沒有關(guān)閉自動旋轉(zhuǎn)時
//UIViewController.m
#pragma mark - rotation
//定義是否支持自動旋轉(zhuǎn)
-(BOOL)shouldAutorotate {
return YES;
}
//定義支持旋轉(zhuǎn)的方向
-(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) {
//橫屏設(shè)置榜配,為防止遮擋鍵盤,調(diào)整輸入視圖的高度
self.textView_height.constant = 50;
}else{
//豎屏設(shè)置
self.textView_height.constant = 200;
}
}
//子視圖
- (void)layoutSubviews {
[super layoutSubviews];
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationMaskPortrait) {
//豎屏布局
} else {
//橫屏布局
}
}
3 設(shè)備方向&屏顯方向
UIDeviceOrientation
機器硬件方向须尚,只讀翅帜,可以通過KVC設(shè)置
[UIDevice currentDevice].orientation
UIInterfaceOrientation
程序界面方向,屬性只讀,有set方法可寫饰豺。
[UIApplication sharedApplication].statusBarOrientation
注意
- 注意彈框View與旋屏
- 橫豎屏分辨率數(shù)
//iOS 8之后
// 豎屏:
UIScreen.mainScreen().bounds: (0.0,0.0,320.0,568.0)
// 橫屏:
UIScreen.mainScreen().bounds: (0.0,0.0,568.0,320.0)