IOS學習 橫豎屏那些事

iOS 6 中的轉屏API發生了變化,以前的控制轉屏的回調已經不再響應:

複製代碼
 1 @interface UIViewController (UIViewControllerRotation)
 2 
 3 // Applications should use supportedInterfaceOrientations and/or shouldAutorotate..
 4 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0);
 5 
 6 // New Autorotation support.
 7 - (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0);
 8 - (NSUInteger)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0);
 9 // Returns interface orientation masks.
10 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0);
複製代碼

以前常用的shouldAutorotateToInterfaceOrientation方法從6.0版本開始,已經被打上DEPRECATED標籤,這意味着原本的應用運行在iOS6設備上是,上面的方法不再被系統調用了。取而代之的是後面標有NS_AVAILABLE_IOS(6_0)的幾個方法,更準確的說,可以用supportedInterfaceOrientations方法代替。需要同時支持iOS 6 和之前版本的設備時,兩個方法都需要實現。

在使用UINavigationController時發現,無論怎麼設置上面方法的返回,都無法控制UINavigationController的旋轉,這似乎是iOS的一個bug。但無論如何,以下是stackflow上面的一個解決方法:

複製代碼
 1 @implementation UINavigationController (Rotation_IOS6)
 2 -(BOOL)shouldAutorotate {
 3     return [[self.viewControllers lastObject] shouldAutorotate];
 4 }
 5 
 6 -(NSUInteger)supportedInterfaceOrientations {
 7     return [[self.viewControllers lastObject] supportedInterfaceOrientations];
 8 }
 9 
10 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
11     return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
12 }
13 @end
複製代碼

-(BOOL)shouldAutorotate 這個函數在UIViewController中時,如果-(NSUInteger)supportedInterfaceOrientations 所支持的方向與app的xx.plist文件中支持的旋轉方向有交集就不走(沒交集就走)。
-(BOOL)shouldAutorotate在UINavigationController中都會走。


發佈了17 篇原創文章 · 獲贊 5 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章