2013-02-20 60 views
3

我正在構建一個應用程序,它只是主視圖(正常和顛倒)的肖像。 我已經在項目設置/ Plist中設置了這個設置,並且一切正常。 但是,我有幾個模態視圖可以用來顯示圖像/視頻,我希望它們能夠旋轉到所有方向。iOS 6 - 導航控制器一些視圖的景觀旋轉而其他人只有肖像

我試着爲UINavigationController添加一個類別,但沒有運氣。 我也加入到調用模式下面的代碼中的viewController:

-(BOOL)shouldAutomaticallyForwardAppearanceMethods{ 
    return NO; 
} 
-(BOOL)shouldAutomaticallyForwardRotationMethods{ 
    return NO; 
} 

我已經添加了下面的代碼,我想允許所有方向的模態viewControllers:

- (BOOL)shouldAutorotate { 
    return YES; 
} 

- (NSUInteger)supportedInterfaceOrientations { 
    return UIInterfaceOrientationMaskAll; 
} 

什麼時我錯過了?有什麼建議麼?

回答

1

您需要爲構建設置中層次結構的祖先viewController啓用旋轉功能(以便允許層次結構中較低層的VC能夠旋轉)。然後從每個viewController中的shouldAutoRotatesupportedInterfaceOrientationswillAnimateRotationToInterfaceOrientation返回適當的邏輯。

返回從shouldAutoRotate的NO否爲根本不移動的viewControllers。

-and同樣,從shouldAutoRotate返回YES和supportedInterfaceOrientations

- 使用willAnimateRotationToInterfaceOrientation返回有效的方位在VC做最後一分鐘的清理或數據更改時,需要出現新的方向之前做。

隨時讓我知道如果你仍然有麻煩。希望能幫助到你!

+0

感謝您的幫助。因此,我啓用了所有方向,然後嘗試在不想旋轉到橫向的視圖中手動設置'supportedInterfaceOrientations',並且它們仍然旋轉!我在「shouldAutoRotate」中也嘗試了yes/no,並且在這些視圖中沒有區別。 – JimmyJammed 2013-02-20 20:55:29

+0

- (BOOL)shouldAutorotate { return NO; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationPortrait; } – 2013-02-20 20:58:41

+1

把它放在你的視圖中,看它是否旋轉。這是我在一個項目中使用的確切代碼,其中一個vc不旋轉但其他人做 – 2013-02-20 20:59:49

3

在iOS 6上,旋轉處理已更改。對於你所描述的問題,有幾種方法。首先,在plist中,啓用除肖像倒置之外的所有方向。

然後,你可以使用下面的解決方案之一:

  1. 使用的UINavigationController的一個子類,它僅允許旋轉爲縱向。
  2. 對於所有控制器,指定它們支持的旋轉。
  3. 您可以重寫應用程序委託中的方法。由於每當方向變化或新的視圖控制器推該方法被調用你的委託時,你可以用它來臨時啓用/禁用橫向顯示爲您的應用程序:

    // In AppDelegate.h: 
    @property (nonatomic) BOOL portraitOnly; 
    
    // In AppDelegate.m: 
    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
    { 
        return _portraitOnly ? UIInterfaceOrientationMaskPortrait : UIInterfaceOrientationMaskAllButUpsideDown; 
    } 
    
    // to switch to portrait only: 
    ((AppDelegate *)[UIApplication sharedApplication].delegate).portraitOnly = YES; 
    
    // to switch to allowing landscape orientations: 
    ((AppDelegate *)[UIApplication sharedApplication].delegate).portraitOnly = NO; 
    
+0

這是個可怕的主意。應用程序的不同部分將應用程序設置爲縱向或不縱向可導致錯誤和難以追蹤的事物。你應該讓該方法返回應用程序支持的所有方向,並讓每個VC實現自己的'-supportedOrientations'方法 – 2013-07-11 20:27:15

+1

@JavierSoto這就是爲什麼它是我提出的* 3 *解決方案的* third *的原因。前兩個更好。我列出了所有這三個問題,以便問題的作者可以選擇最適合手頭問題的問題。 – 2013-07-12 10:12:22

+0

如果將B推到A上,A是縱向,B是橫向,這似乎不起作用。 UINavigationController似乎只關心根的方向,所以後續的視圖控制器沒有遵守它們的首選方向。這是你的經歷嗎? – Crashalot 2017-01-04 19:38:21

相關問題