2013-01-22 50 views
1

我覺得我的問題已在iOS6: supportedInterfaceOrientations not working (is invoked but the interface still rotates)中得到解答,但我沒有深入瞭解如何實現這些響應。如何旋轉tabbarcontroller中的一個視圖?

我正在寫一個使用iOS6的選項卡式應用程序,並且我希望其中一個視圖控制器可以旋轉,而其餘的可以保持放置。我知道我需要使用supportedInterfaceOrientations,但我不知道如何或在何處使用它,並且我還無法破譯Apple文檔。 UITabBarController被設置爲AppDelegate中的根視圖控制器,如果這是相關的。

在此先感謝您的幫助。

+0

我要投票這件事。我試圖弄清楚這一點,並在這裏得到沒有牽引在這個相同的問題 –

回答

1

實現你的子類的UITabBarController旋轉supportedInterfaceOrientations整個的UIView。下面是其中的第一個視圖控制器不會自轉了人像的一個例子:

- (NSUInteger)supportedInterfaceOrientations { 
    if (self.selectedIndex == 0) 
     return UIInterfaceOrientationMaskPortrait ; 
    else 
     return UIInterfaceOrientationMaskAll ; 
} 

但是,如果切換到視圖控制器#2(索引1),旋轉爲橫向,並切換回視圖控制器# 1,你會看到View Controller#1以橫向顯示。我正在尋找解決方案。

+0

謝謝 - 做到了! –

-1

一個簡單的方法來實現這一目標是正確,並在您的viewDidLoad結束加載您的看法:方法使用轉換

[self.view setTransform:CGAffineTransformMakeRotation(-M_PI/2)]; 
2

這個問題在一個星期內讓我失望。我有完全相同的情況,只希望能夠旋轉一個視圖。您可以在選項卡式應用程序中避開它的方式是註冊定位通知。在您的TabController(以及其他superViews)和您要旋轉的VC中返回NO for shouldAutoRotate。然後,在viewWillAppear中註冊通知:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)name:UIDeviceOrientationDidChangeNotification 
      object:[UIDevice currentDevice]]; 

而且你的函數被調用看起來就像這樣:

- (void) orientationChanged:(NSNotification *)note 
{ 
    UIDevice * device = note.object; 
    switch(device.orientation) 
    { 
     case UIDeviceOrientationPortrait: 
      break; 

     case UIDeviceOrientationPortraitUpsideDown: 
      break; 
     case UIDeviceOrientationLandscapeLeft: 
      if(viewHasAppeared==true){ 
       [self performSegueWithIdentifier:@"firstToLandscape" sender:self];} 
      break; 
     case UIDeviceOrientationLandscapeRight: 
      if(viewHasAppeared==true){ 
       [self performSegueWithIdentifier:@"firstToLandscape" sender:self];} 
      break; 
     default: 
      break; 
    }; 
} 

-put ViewDidAppear內一個布爾值,因爲你不能Segue公司,直到上實際的看法。

現在,如果您在橫向模式下更改的視圖不是標籤式視圖控制器,那麼處理該問題的最佳方式是隻允許在該視圖中使用AutoRotation,然後在willAnimate中將像這樣解除VC:

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) 
    { 
     [self dismissViewControllerAnimated:YES completion:nil];  
    } 
} 

如果是標籤式查看,那麼只需在該視圖中註冊orientationchanges。但是,確保在離開視圖時刪除orientationChange通知。如果你不這樣做,那麼你將會從其他viewController中尋找景觀。

當你離開去到另一個選項卡,你需要有實現的tabBarController代表,並確保您刪除觀察者,像這樣:

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController 
{ 
    if(![NSStringFromClass([viewController class])isEqualToString:@"FirstViewController"]) 
    { 
      [[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];    
    } 

} 
+0

您的解決方案是否與我的問題一樣?(http://www.youtube.com/watch?v=ycKTHjxh438)?我試圖始終保持「第一視圖」的肖像。這是正確的,第一視圖本身永遠不會旋轉出肖像,但這是不正確的,因爲如果我只是在橫向上查看第二視圖,它會出現在橫向。 –

+0

根本不是真的。我只是編輯它來添加你刪除方向觀察者的部分,所以你只能切換到第一個VC中的風景。檢查itttttttttttttttttttttt –

+0

我認爲你已經回答了一個不同的問題。您的示例演示瞭如何在設備旋轉到橫向時呈現新的模式視圖控制器,如Apple的Calendar應用程序在橫向顯示「星期」視圖。我相信喬爾的問題是「我有一個標籤欄控制器,我希望標籤的視圖控制器之一在設備旋轉時不旋轉。」 –