一、基本说明
UIDeviceOrientation 是机器硬件的当前旋转方向 这个你只能取值 不能设置
UIInterfaceOrientation 是你程序界面的当前旋转方向 这个可以设置
Portrait 表示 纵向,Landscape 表示 横向。
typedef enum { UIDeviceOrientationUnknown, UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left UIDeviceOrientationFaceUp, // Device oriented flat, face up UIDeviceOrientationFaceDown // Device oriented flat, face down } UIDeviceOrientation; typedef enum { UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight, UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft //注意Interface和Device的左右是相反的} UIInterfaceOrientation; #define UIDeviceOrientationIsPortrait(orientation) ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown) #define UIDeviceOrientationIsLandscape(orientation) ((orientation) == UIDeviceOrientationLandscapeLeft || (orientation) == UIDeviceOrientationLandscapeRight)判断设备现在的方向:
C代码 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { //宣告一個UIDevice指標,並取得目前Device的狀況 UIDevice *device = [UIDevice currentDevice] ; //取得當前Device的方向,來當作判斷敘述。(Device的方向型態為Integer) switch (device.orientation) { case UIDeviceOrientationFaceUp: NSLog(@"螢幕朝上平躺"); break; case UIDeviceOrientationFaceDown: NSLog(@"螢幕朝下平躺"); break; //系統無法判斷目前Device的方向,有可能是斜置 case UIDeviceOrientationUnknown: NSLog(@"未知方向"); break; case UIDeviceOrientationLandscapeLeft: NSLog(@"螢幕向左橫置"); break; case UIDeviceOrientationLandscapeRight: NSLog(@"螢幕向右橫置"); break; case UIDeviceOrientationPortrait: NSLog(@"螢幕直立"); break; case UIDeviceOrientationPortraitUpsideDown: NSLog(@"螢幕直立,上下顛倒"); break; default: NSLog(@"無法辨識"); break; } // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft); // 只支持向左横向, YES 表示支持所有方向 }或者
C代码 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; if (UIDeviceOrientationIsLandscape(deviceOrientation)) NSLog(@"横向"); else if(UIDeviceOrientationIsPortrait(deviceOrientation)) NSLog(@"纵向"); // // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft); // 只支持向左横向, YES 表示支持所有方向 }二、使用场景
如果App的所有切面都要支持横竖屏的切换,那只需要勾选【General】 中的【Device Orientation】,选择希望支持的方向即可。
图中支持竖屏和Home在右侧如上设置完之后,当设备竖屏的时候,所有的界面都是竖屏显示的;而当设备横屏Home在右侧时,所有的界面会横屏显示。其他方向不支持,界面不会改变。
这里有个坑:在iOS 9 之后横屏时,状态栏会消失。解决方法:确保plist 中的【View controller-based status bar appearance】为YES,然后重写ViewController的 - (BOOL)PRefersstatusBarHidden
,返回值是NO。
- (BOOL)prefersStatusBarHidden{ return NO;}2、特殊情形,个别界面固定方向,其他所有界面都支持横竖屏切换这种情况,在【General】-->【Device Orientation】中设置好支持的方向后,只需要在这些特殊的固定方向的视图控制器中重写两个方法:
// 支持设备自动旋转- (BOOL)shouldAutorotate{ return YES;}/** * 设置特殊的界面支持的方向,这里特殊界面只支持Home在左侧的情况*/- (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeRight;}3、特殊情形:个别界面支持横竖屏切换,其他所有界面都固定方向可能大多数App会是这种需求,某些特殊界面只能横屏,如视频播放类App。这里有两种处理方式:方式一在【General】-->【Device Orientation】中设置好需要支持的所有方向。然后使用一个基类控制器,在基类控制器中重写两个控制横竖屏的方法:
// 支持设备自动旋转- (BOOL)shouldAutorotate{ return YES;}// 支持竖屏显示- (UIInterfaceOrientationMask)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskPortrait;}再然后,特殊的界面上再重写这俩方法,让其可以自动切换方向。
// 如果需要横屏的时候,一定要重写这个方法并返回NO
- (BOOL)prefersStatusBarHidden{ return NO;}// 支持设备自动旋转- (BOOL)shouldAutorotate{ return YES;}// 支持横屏显示- (UIInterfaceOrientationMask)supportedInterfaceOrientations{ // 如果该界面需要支持横竖屏切换 return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait; // 如果该界面仅支持横屏 // return UIInterfaceOrientationMaskLandscapeRight;}方式二用方式一的方法,还需要借助一个基类,所有的控制器都要继承这个基类,太麻烦?另一种方式,是借助通知来控制界面的横竖屏切换。还是整个App中大部分界面都是竖屏,某个界面可以横竖屏切换的情况。
首先,在【General】-->【Device Orientation】设置仅支持竖屏,like this:
Device Orientation然后在特殊的视图控制器里的ViewDidLoad中注册通知:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];通知方法的实现过程:
- (void)deviceOrientationDidChange{ NSLog(@"deviceOrientationDidChange:%ld",(long)[UIDevice currentDevice].orientation); if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) { [[UIapplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]; [self orientationChange:NO]; //注意: UIDeviceOrientationLandscapeLeft 与 UIInterfaceOrientationLandscapeRight } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) { [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]; [self orientationChange:YES]; }}- (void)orientationChange:(BOOL)landscapeRight{ if (landscapeRight) { [UIView animateWithDuration:0.2f animations:^{ self.view.transform = CGAffineTransformMakeRotation(M_PI_2); self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); }]; } else { [UIView animateWithDuration:0.2f animations:^{ self.view.transform = CGAffineTransformMakeRotation(0); self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); }]; }}// 用到的两个宏: #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)最重要的一点:需要重写如下方法,并且返回NO。
- (BOOL)shouldAutorotate{ return NO;}这样,在设备出于横屏时,界面就会变成横屏,设备处于竖屏时,界面就会变成竖屏。
填坑
上面方式二,因为【General】-->【Device Orientation】因为只设置了竖屏,所以当横屏时,如果有键盘弹出,键盘是竖屏时的样式。解决办法:在【General】-->【Device Orientation】中加上横屏时的方向。
如果VieController 是放在UINavigationController或者UITabBarController中,需要重写它们的方向控制方法。
// UINavigationController:- (BOOL)shouldAutorotate{ return [self.topViewController shouldAutorotate];}- (UIInterfaceOrientationMask)supportedInterfaceOrientations{ return [self.topViewController supportedInterfaceOrientations];}// UITabBarController:- (BOOL)shouldAutorotate{ return [self.selectedViewController shouldAutorotate];}- (UIInterfaceOrientationMask)supportedInterfaceOrientations{ return [self.selectedViewController supportedInterfaceOrientations];}如果想要点击某个按钮之后,强制将竖屏显示的界面变成横屏呢?有人可能会想到这样写:
// 横屏- (IBAction)landscapAction:(id)sender { [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]; [self orientationChange:YES];}但是按照上面的写法,会导致返回到之前的界面时,视图方向错误,即使返回前执行如下代码:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];[self orientationChange:NO];也没有作用,下面是在开源工程中无意看到的写法:
// 横屏- (IBAction)landscapAction:(id)sender { [self interfaceOrientation:UIInterfaceOrientationLandscapeRight];}// 竖屏- (IBAction)portraitAction:(id)sender { [self interfaceOrientation:UIInterfaceOrientationPortrait];}- (void)interfaceOrientation:(UIInterfaceOrientation)orientation{ if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { SEL selector = NSSelectorFromString(@"setOrientation:"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:[UIDevice currentDevice]]; int val = orientation; [invocation setArgument:&val atIndex:2]; [invocation invoke]; }}上面的方法会将设备的方向强制设置为某个方向,然后再监控设备方向改变的通知,即可实现横竖屏切换。
参考:http://justcoding.iteye.com/blog/1472932http://www.cocoachina.com/ios/20160722/17148.html
新闻热点
疑难解答