2012-11-15 24 views
0

我希望所有視圖都處於縱向模式。除了當我將UINavigationController推到另一個上時,這種方法是有效的。在這種情況下,secondaryNavigationController內部的視圖將堅持設備方向。以下是我打電話給UINavigationControllers的方法。嵌套的UINavigationController中的視圖方向不正確

[secondaryNavigationController setNavigationBarHidden:YES]; 
[[appDelegate secondaryNavigationController navigationBar] setHidden:YES]; 
[mainNavigationController presentModalViewController:[appDelegate secondaryNavigationController] animated:YES]; 

我所有的觀點都實現了這種方法,但它似乎沒有幫助。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
+1

使用此答案[UINavigationController的我不能停止旋轉] [1] [1]:http://stackoverflow.com/questions/13333429/modal -uinavigationcontroller-I-着停轉/ 13334446#13334446 – Janub

回答

0

下面是對我有用的完整答案,基於iYaniv的評論和Jacky Boy的回答。

我把iYaniv的類別作爲UINavigationController的子類而不是類別(這是你應該做的)。然後我將UINavigationController *的所有實例替換爲myUINavigationController *。然後在我的所有UIViewControllers中使用了Jacky Boy的片段。

/* myUINavigationController.h */ 

#import <UIKit/UIKit.h> 

@interface myUINavigationController : UINavigationController 

@end 

/* myUINavigationController.m */ 

#import "myUINavigationController.h" 

@interface myUINavigationController() 

@end 

@implementation myUINavigationController 

-(BOOL)shouldAutorotate 
{ 
    return [[self.viewControllers lastObject] shouldAutorotate]; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return [[self.viewControllers lastObject] supportedInterfaceOrientations]; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; 
} 

@end 
1

慎用:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

Since it has been deprecated on iOS 6.0。目前這裏有一些問題,UIViewControllers的旋轉在UINavigationControllersUITabBarControllers之內。爲了解決這個問題,你應該分類UINagivationController或爲它創建一個類別(儘管蘋果不贊成第二個)。您可以使用this(這種情況是爲UITabBarController,但您可以理解邏輯),以檢查如何執行子分類。然後,你可以做你的UIViewControllers如下:

-(BOOL)shouldAutorotate 
{ 
    return YES; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

儘管您允許旋轉(返回是),UIViewController總是會在肖像。這裏的邏輯是,如果你來自Landscape上的UIViewController,如果你返回NO,你的UIViewController將停留在Landscape中。