2014-09-10 25 views
0

此時我已將ViewControllerA保持爲縱向模式。爲此,我正在使用preferredInterfaceOrientationForPresentationiOS 7在解除推送時強制縱向定位

這可確保ViewControllerA在推入時處於縱向模式。但是,如果我從ViewControllerA推送到ViewControllerB,切換到ViewControllerB中的橫向模式,,然後退回到ViewControllerA,ViewControllerA可以以橫向模式呈現。我的願望是繼續ViewControllerB的多方位支持,但強制ViewControllerA自動旋轉到肖像。

此外,出於某種原因shouldAutorotate似乎不被調用ViewControllerA。也許解決這個問題可以解決整個問題?

的UINavigationController + Orientation.h類別

@interface UINavigationController (Orientation) 

- (BOOL)shouldAutorotate; 
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation; 

@end 

的UINavigationController + Orientation.m類別

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

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

@end 

ViewControllerA.m

- (BOOL)shouldAutorotate { 
    return YES; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { 
    return UIInterfaceOrientationPortrait; 
} 

回答

0

好的,我能夠使事情工作!

所以在我的情況下,讓事情變得困難的一件事是我有兩個部分;一個發生在登錄之前(沒有NavController)另一個登錄後(確實有NavController)。也許我在那裏打破了最佳實踐指南?

不管怎麼說,張志賢C.的答案是一個幫助我:我通過使一個新的空白項目取得這一發現iOS 7. Change page orientation only for one view controller

。如果您在強制方向上遇到問題,可以看看我製作的示例https://github.com/jerherrero/Force-Orientations

1

我面臨同樣的問題。我的解決方案在下面,它在一個已發佈的應用程序中。

我增加了一個叫做allowViewRotation一個公共屬性,以我的AppDelegate.h:

@property (assign, nonatomic) BOOL allowViewRotation; 

然後,我已經實現了以下調用我的AppDelegate.m文件:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ 
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { // if iPad 
     return UIInterfaceOrientationMaskAll; 
    } 

    if (self.allowViewRotation) { 
     return UIInterfaceOrientationMaskAllButUpsideDown; 
    } 
    else { 
     return UIInterfaceOrientationMaskPortrait; 
    } 
} 

現在ViewControllerA集allowViewRotation屬性爲NO

((AppDelegate*)[UIApplication sharedApplication].delegate).allowViewRotation = NO; 

而在ViewControllerB中設置allowView旋轉屬性爲YES

((AppDelegate*)[UIApplication sharedApplication].delegate).allowViewRotation = YES; 

注:請確保您啓用了正確的設備方位部署信息

enter image description here

希望這有助於!

+0

它仍然不起作用。我正在使用與您所做的相同的方向設置(除了顛倒以外)。我嘗試在viewDidLoad和viewWillAppear中都使用**((AppDelegate *)[UIApplication sharedApplication] .delegate).allowViewRotation **,但兩者都沒有阻止ViewControllerA在退回時加載爲橫向格式。 – 2014-09-10 16:50:29

+0

確保默認值是allowViewRotation = NO。並在ViewControllerB的** viewWillAppear:**和** viewWillDisappear裏面加上YES/NO改變:**' - (void)viewWillAppear:(BOOL)animated動畫 {super viewWillAppear:animated]; appDelegate.allowViewRotation = YES; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; appDelegate.allowViewRotation = NO; }' – Mike 2014-09-10 17:43:54

+0

嘗試調用attemptRotationToDeviceOrientation來強制應用程序調用** supportedInterfaceOrientationsForWindow ** https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/clm/UIViewController/attemptRotationToDeviceOrientation – Mike 2014-09-10 17:53:37