選擇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;