首先我們要先設(shè)置app允許的窗口朝向
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window;
如果你沒(méi)有寫這個(gè)方法伺帘,那會(huì)從plist里獲取默認(rèn)值。
該方法來(lái)自UIApplicationDelegate新锈,所以要寫在AppDelegate里。
所使用的枚舉
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait,//home鍵朝下
UIInterfaceOrientationMaskLandscapeLeft,//home鍵朝左
UIInterfaceOrientationMaskLandscapeRight ,//home鍵朝右
UIInterfaceOrientationMaskPortraitUpsideDown,//home鍵朝上
UIInterfaceOrientationMaskLandscape,//home鍵朝左或者朝右
UIInterfaceOrientationMaskAll,//上下左右都可以
UIInterfaceOrientationMaskAllButUpsideDown,//home鍵朝左朝右朝下
}
swift寫法
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait.union(UIInterfaceOrientationMask.landscapeLeft);
//可以豎屏和home鍵在左邊橫屏
}
OC寫法
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}
然后設(shè)置頁(yè)面是否允許翻轉(zhuǎn)和頁(yè)面允許的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
//默認(rèn)值iPad UIInterfaceOrientationMaskAll iPhone UIInterfaceOrientationMaskAllButUpsideDown
- (BOOL)shouldAutorotate //默認(rèn)值為YES
- (BOOL)prefersStatusBarHidden //默認(rèn)值為YES
supportedInterfaceOrientations這個(gè)方法表示該view controller允許的朝向,但是這個(gè)方向必須是application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window中或者plist中允許的方向亥至。
橫屏的時(shí)候狀態(tài)欄會(huì)默認(rèn)隱藏。所以我們需要設(shè)置prefersStatusBarHidden
OC
- (BOOL)shouldAutorotate {
return YES;
}
- (BOOL)prefersStatusBarHidden {
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
Swift
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait.union(UIInterfaceOrientationMask.landscapeRight.union(UIInterfaceOrientationMask.portraitUpsideDown))
}
override var prefersStatusBarHidden: Bool {
return false
}
這三個(gè)方法都是UIViewController下贱迟。也就是說(shuō)UIViewcontroller及他的子類都可以用這三個(gè)方法(UITabBarController, UINavigationController也是UIViewcontroller的子類)姐扮。
如果rootViewController為UITabBarController實(shí)例的時(shí)候。
只有UITabBarController的shouldAutorotate起作用衣吠。UITabBarController下的UINavigationController和UIViewcontroller的shouldAutorotate都有不能控制窗口翻轉(zhuǎn)茶敏,但是UIViewcontroller的shouldAutorotate為YES時(shí)可以造成狀態(tài)欄的翻轉(zhuǎn),狀態(tài)欄的翻轉(zhuǎn)方向受UIViewcontroller的supportedInterfaceOrientations影響缚俏。
效果如下:
如果rootViewController為UINavigationController實(shí)例的時(shí)候惊搏。UIViewcontroller的shouldAutorotate為YES時(shí)可以造成狀態(tài)欄的翻轉(zhuǎn)但是不會(huì)對(duì)窗口的翻轉(zhuǎn)有影響贮乳。狀態(tài)欄的翻轉(zhuǎn)方向受UIViewcontroller的supportedInterfaceOrientations影響。
由UIViewcontroller present出來(lái)的頁(yè)面不受rootViewController的影響恬惯。翻轉(zhuǎn)情況由他自己的shouldAutorotate和supportedInterfaceOrientations控制向拆。
強(qiáng)制翻轉(zhuǎn):
當(dāng)退出可翻轉(zhuǎn)頁(yè)面的時(shí)候我們往往會(huì)用到強(qiáng)制翻轉(zhuǎn)。原理就是一個(gè)KVC酪耳。
OC
//先判斷Orientation狀態(tài)
if ((long)self.preferredInterfaceOrientationForPresentation != 1) {
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
Swift
//注意在swift中系統(tǒng)不能將enum直接轉(zhuǎn)成int型浓恳,所以需要手動(dòng)get rawValue
if self.preferredInterfaceOrientationForPresentation != .portrait {
UIDevice.current.setValue(NSNumber.init(value: UIDeviceOrientation.unknown.rawValue), forKey: "orientation")
UIDevice.current.setValue(NSNumber.init(value: UIDeviceOrientation.portrait.rawValue), forKey: "orientation")
}