2012-09-13 106 views
36

我有這個問題與iOS 6 SDK:我有一些意見,應該允許旋轉(例如一個視頻),有些不該。 現在我明白我必須檢查應用程序的Info.plist中的所有方向,然後在每個ViewController中進行排序,應該發生什麼。但它不工作!該應用始終旋轉到Info.plist中給出的方向。iOS 6旋轉:supportedInterfaceOrientations不起作用?

的Info.plist:

<key>UISupportedInterfaceOrientations</key> 
    <array> 
     <string>UIInterfaceOrientationPortrait</string> 
     <string>UIInterfaceOrientationLandscapeLeft</string> 
     <string>UIInterfaceOrientationLandscapeRight</string> 
    </array> 

是shouldn't被允許旋轉的任何視圖控制器:

//deprecated 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (NSUInteger)supportedInterfaceOrientations{ 
return UIInterfaceOrientationMaskPortrait; 
} 

觀察:應用旋轉到橫向和縱向。任何想法爲什麼或我做錯了什麼?

乾杯, 馬克

編輯:我最近的研究結果還表明,如果你想在你的應用某些點有旋轉,你來激活您的項目設置或信息全部四個旋轉方向的.plist。此替代方案是替代您的AppDelegate中的

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 

,這會覆蓋Info.plist。現在不可能只在你的Info.plist中設置Portrait,然後通過重寫shouldAutorotateToInterfaceOrientation或supportedInterfaceOrientations在某個ViewController中進行旋轉。

+0

這是根據NDA,在蘋果開發論壇發佈,我可以幫助。 –

+0

這樣更好嗎?我想現在沒有人會在乎傷害NDA了...... – stk

+0

它仍然是一個NDA,我也猜測這是一個常見問題,已經在開發論壇已經完全回答了 – wattson12

回答

31

如果您的ViewController是UINavigationController或UITabBarController的子項,那麼它就是您的問題的父項。您可能需要子類化父視圖控制器,只是重寫那些InterfaceOrientation方法,你已經在你的問題出

編輯:

舉例肖像僅TabBarController

  @interface MyTabBarController : UITabBarController 
      { 
      } 
      @end 

      @implementation MyTabBarController 

      // put your shouldAutorotateToInterfaceOrientation and other overrides here   
      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
       return (interfaceOrientation == UIInterfaceOrientationPortrait); 
      } 

      - (NSUInteger)supportedInterfaceOrientations{ 
       return UIInterfaceOrientationMaskPortrait; 
      } 

     @end 
+4

,你應該不會忘記 - supportedInterfaceOrientations方法,因爲在iOS6中,shouldAutorotate ...不再使用! – stk

-2

作爲替代方法,如果你想在你的應用中保留iOS6以前的旋轉功能:

以下是github上一段有用的代碼,它調用iOS6的方法調用,以便旋轉的工作方式與iOS4/iOS4相似。這對我非常有幫助,因爲我支持一個真正微型管理其旋轉的傳統應用程序。實施iOS6所需的更改本來是很多工作。榮譽發佈給用戶。

https://gist.github.com/3725118

+1

這是一個非常非常糟糕的主意。只是在說'。 – steipete

27

添加到上述CSmith:它的答案,在一個UINavigationController子類下面的代碼允許代表團中,我預計這擺在首位的工作方式的頂視圖控制器:

- (BOOL)shouldAutorotate; 
{ 
    return YES; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    if ([[self topViewController] respondsToSelector:@selector(supportedInterfaceOrientations)]) 
     return [[self topViewController] supportedInterfaceOrientations]; 
    else 
     return [super supportedInterfaceOrientations]; 
} 
+5

這完美的作品!爲什麼這不是由Apple IOS SDK完成的? :( – flypig

+5

@flypig,過去是這樣的,Apple iOS SDK的行爲與之前的完全相同,他們在iOS 6中沒有任何明顯的原因將其改爲新系統。 –

8

這是CSmith方法的另一種選擇。

如果你要複製的預iOS 6的行爲,其中在導航堆棧/標籤欄的所有意見必須在允許的組取向的同意,把這個在您的UITabBarControllerUINavigationController子類:

- (NSUInteger)supportedInterfaceOrientations 
{ 
    NSUInteger orientations = [super supportedInterfaceOrientations]; 

    for (UIViewController *controller in self.viewControllers) 
     orientations = orientations & [controller supportedInterfaceOrientations]; 

    return orientations; 
} 
2

嘗試添加此類別:

@interface UINavigationController(InterfaceOrientation) 

@end 

@implementation UINavigationController(InterfaceOrientation) 

- (NSUInteger) supportedInterfaceOrientations { 
    if (self.viewControllers.count > 0) 
     return [[self.viewControllers objectAtIndex:0] supportedInterfaceOrientations]; 
    else 
     return UIInterfaceOrientationMaskAll; 
} 

@end 
+0

很好,任何子類化!謝謝!但我更喜歡使用當前顯示的數組中的最後一個視圖控制器。 – HotJard

-1

@Shimanski阿爾喬姆的答案是好的,但我想用最頂層(當前可見)控制器更好的解決辦法:

@interface UINavigationController(InterfaceOrientation) 

@end 

@implementation UINavigationController(InterfaceOrientation) 

- (NSUInteger) supportedInterfaceOrientations { 
    if (self.viewControllers.count > 0){ 
     return [[self.viewControllers objectAtIndex:[self.viewControllers count] - 1] supportedInterfaceOrientations]; 
    } 
    return UIInterfaceOrientationMaskAll; 
} 

@end 
0

進一步加入@CSmith和@EvanSchoenberg。

如果您有一些旋轉的視圖,並且某些視圖沒有旋轉,您必須創建一個UITabBarController的自定義實例,但仍然會讓每個UIViewController決定。

- (BOOL)shouldAutorotate; 
{ 
    return YES; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    UIViewController * top; 
    UIViewController * tab = self.selectedViewController; 
    if([tab isKindOfClass: 
     ([UINavigationController class])]) { 
     top = [((UINavigationController *)tab) 
       topViewController]; 
    } 

    if ([top respondsToSelector:@selector(supportedInterfaceOrientations)]) 
     return [top supportedInterfaceOrientations]; 
    else 
     return [super supportedInterfaceOrientations]; 
} 
0

對於使用UINavigationController和Swift的用戶,您可以將此擴展添加到您的項目中。之後,導航控制器將控制委派給他們的子控制器。

extension UINavigationController { 
    override public func supportedInterfaceOrientations() 
    -> UIInterfaceOrientationMask { 
     if let ctrl = topViewController { 
      return ctrl.supportedInterfaceOrientations() 
     } 
     return super.supportedInterfaceOrientations() 
    } 

    override public func shouldAutorotate() -> Bool { 
     if let ctrl = topViewController { 
      return ctrl.shouldAutorotate() 
     } 
     return super.shouldAutorotate() 
    } 
}