选择Project->Target->General->Deployment Info->Device Orientation,勾选须要支持的屏幕方向,好比 :fa-square-o:Portrait :fa-square-o:Upside Down :fa-square-o:Landscape Left :fa-square-o:Landscape Rightapp
在AppDelegate.m中添加方法:ide
//IOS6及以上 - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window NS_AVAILABLE_IOS(6_0){ return UIInterfaceOrientationMaskPortrait; }
主要依赖于如下几个函数函数
//当前viewcontroller是否支持转屏 - (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0); //当前viewcontroller支持哪些转屏方向 - (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0); //当前viewcontroller默认的屏幕方向 -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0);
UIInterfaceOrientationMask ->UIInterfaceOrientation的多种组合code
UIInterfaceOrientationMaskPortrait //竖屏 UIInterfaceOrientationMaskLandscapeLeft //左横屏 UIInterfaceOrientationMaskLandscapeRight //右横屏 UIInterfaceOrientationMaskPortraitUpsideDown //竖屏(颠倒) UIInterfaceOrientationMaskLandscape //横屏 UIInterfaceOrientationMaskAll //全部状态 UIInterfaceOrientationMaskAllButUpsideDown //除颠倒竖屏外
<1>调用于翻转以前视频
//通常用来禁用某些控件或者中止某些正在进行的活动,好比中止视频播放。get
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration NS_DEPRECATED_IOS(2_0,8_0); - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);
<2>调用于翻转的过程当中it
//通常用来定制翻转后各个控件的位置、大小等。io
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration NS_DEPRECATED_IOS(3_0,8_0); - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);
<3>调用于整个翻转完成以后。方法
//通常用来从新启用某些控件或者继续翻转以前被暂停的活动,好比继续视频播放im
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation NS_DEPRECATED_IOS(2_0,8_0);
1. UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; 2. UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;