2012-09-22 34 views
3

我有一個UITabBarViewController肖像模式,我推動modalViewController橫向。當我按下模式時,旋轉會變成風景,但是當我解散它時,方向會保持在風景中而不是恢復爲人像。iOS 6恢復方向後關閉ModalViewController

代碼:

一個CustomUINavigationController爲風景模式:

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

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

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

中的UITabBarController

- (BOOL)shouldAutorotate 
{ 
    if ([self inModal]) { 
    UINavigationController *nav = (UINavigationController *)self.modalViewController; 
    return [[nav.viewControllers lastObject] shouldAutorotate]; 
    } else { 
    return NO; 
    } 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    if ([self inModal]) { 
    UINavigationController *nav = (UINavigationController *)self.modalViewController; 
    return [[nav.viewControllers lastObject] supportedInterfaceOrientations]; 
    } else { 
    return UIInterfaceOrientationPortrait; 
    }  
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    if ([self inModal]) { 
    UINavigationController *nav = (UINavigationController *)self.modalViewController; 
    return [[nav.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; 
    } else { 
    return UIInterfaceOrientationPortrait; 
    } 
} 

- (BOOL)inModal 
{ 
    if (self.modalViewController && [self.modalViewController isKindOfClass:[UINavigationController class]]) { 
    return YES; 
    } else { 
    return NO; 
    }  
} 

如果我設置shouldRotate爲YES我得到一個錯誤:*終止應用程序由於未捕獲異常'UIApplicationInvalidInterfaceOrientation',原因:'支持的方向與應用程序沒有共同的方向,並且shouldAutorotate返回YES。儘管我已經在UISupportedDeviceOrientations中設置了兩個方向。

回答

6

我覺得你的問題是由這一行(你在你的代碼有兩次)引起的:

return UIInterfaceOrientationPortrait; 

目前,我想要做的某事物類似於你試圖實現的,並且在這裏問了我的問題:iOS 6 auto rotation issue - supportedInterfaceOrientations return value not respected

你的代碼的問題是兩個方法不返回一個接口方向,而是一個掩碼,它的位編碼允許的組合或優選的方向。

嘗試這種情況:

return UIInterfaceOrientationMaskPortrait; 

UIInterfaceOrientationPortrait儘可能遠映射到0我所知,並且因此解釋爲空集接口取向(無)。