前要
今天偶然測試我們app的我的手機默認開啓了自動旋轉的功能,我正好把手機橫屏放着,這個時候真機測試的時候,整個UI的界面也是橫屏的,很奇怪的明明UIViewController
根控制器和UINavigationController
導航控制器以及UITabBarController
標籤控制器的都設置了不支持橫屏的。
UITabBarController和UINavigationController
- (BOOL)shouldAutorotate
{
return [self.selectedViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
UIViewControler
- (BOOL)shouldAutorotate{
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
問題分析
只要手機的物理方向是橫屏狀態的下打開app,就會出現app是橫屏狀態的,整個UI界面就會橫屏的狀態,首先猜測既然橫屏的方向,那麼能不能強制豎屏呢?在app的代理的入口處直接設置
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];//此方法
發現還是不行,手機橫屏狀態下打開的話還是橫屏的佈局的UI界面的,後來我突然想到是不是進入app的第一個界面的控制器的影響的,我看了一下我的app打開首先進入的第一個界面,由於app首先打開會進入一個代替系統的啓動頁的控制器的界面,類似於啓動頁上面加個廣告頁的業務需求,每次都會先進入這個展示廣告的啓動頁。果然只要設置這個展示廣告的頁面只支持豎屏,最後完美解決了。
- (BOOL)shouldAutorotate{
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
最終總結
最重要的打開了手機app的自動旋轉的功能,否則你就是橫屏的情況下也不會有任何作用,因爲系統直接把你的手機固定爲豎屏。
1.app的代理方法中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//防止手機橫屏時出現的界面橫屏佈局
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];//此方法
}
2.app打開時,第一個展示的控制器一定要強制只支持豎屏,不支持自動旋轉。
最後完美解決,自動旋轉打開下,橫屏手機打開app,界面出現橫屏的情況。