2013-04-05 62 views
2

我的應用程序包含兩個表視圖控制器。在第一個我希望視圖能夠左右旋轉(除了肖像模式),但在第二個表視圖控制器(我從第一個表中點擊單元格後導航到)我想要它只能在肖像模式下查看。我試過這個代碼,但它沒有工作,它一直在旋轉。限制某些視圖的自轉

- (NSUInteger)supportedInterfaceOrientations { 
    return UIInterfaceOrientationMaskPortrait; 
} 

- (BOOL) shouldAutorotate { 
    return NO; 
} 

注意:我確實從項目目標的摘要選項卡啓用了左/右/縱向方向。任何修復?

+0

檢查此「http://stackoverflow.com/questions/12772749/support-different-orientation-for-only-one-view-ios-6」 – Nandha 2013-04-05 13:27:46

回答

2

創建的UINavigationController一個類別,它包括以下方法:

(兩者爲iOS 6和iOS 5作品)

- (BOOL)shouldAutorotate 
    { 
     return self.topViewController.shouldAutorotate; 
    } 

    - (NSUInteger)supportedInterfaceOrientations 
    { 
     return self.topViewController.supportedInterfaceOrientations; 
    } 

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { 
     return [self.topViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]; 
    } 

然後在控制器

首先實現這些方法:

- (BOOL)shouldAutorotate { 
    return YES; 
} 

- (NSUInteger)supportedInterfaceOrientations { 
    if (RUNNING_IPAD) { 
     return UIInterfaceOrientationMaskAll; 
    } 
    else { 
     return UIInterfaceOrientationMaskAllButUpsideDown; 
    }; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { 
    if (RUNNING_IPAD) { 
     return YES; 
    } 
    else { 
     return toInterfaceOrientation != UIInterfaceOrientationMaskPortraitUpsideDown; 
    } 
} 

第二個:

- (BOOL)shouldAutorotate { 
    return NO; 
} 

- (NSUInteger)supportedInterfaceOrientations { 
     return UIInterfaceOrientationMaskPortrait; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { 
     return NO; 
} 

項目的旋轉設置應該是這樣的:

Settings

+0

謝謝!我確實找到了一個更簡單的答案,但我選擇了這個,因爲它支持ios 5和6,我的只支持6。 – HusseinB 2013-04-05 14:14:12

0

其實我發現我的問題的解決方案:

-(BOOL)shouldAutorotate{ 
    return YES; 
} 

-(NSInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

這將限制期爲縱向即使您在項目的Traget的摘要選項卡中切換左/右方向。