0

我的主視圖控制器是UITabBarController,其中4個UINavigationControllers作爲標籤欄項目。每個導航欄都是幾個不同的視圖,它們被壓入堆棧。UITabBarController和UINavigationController中的自動旋轉視圖

我有一個單一的視圖,我想自動旋轉,但我不能讓willAnimateFirstHalfOfRotationToInterfaceOrientation在視圖控制器內被調用。

我試着子類我UITabBarControllerUINaviationControllers並添加覆蓋了shouldAutorotateToInterfaceOrientation像這樣:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 

即使在那之後我不能得到的視圖旋轉。我想也許最上面的UIViewController(包括標籤和導航)必須能夠旋轉。甚至是連鎖店的每一個觀點。我試過爲每個控制器重寫shouldAutorotateToInterfaceOrientation,但仍然無法旋轉視圖。

任何人都完成了這個或有任何建議嗎?

在此先感謝!

回答

1

除了繼承你的tabBarController因爲你是壓倒一切的houldAutorotateToInterfaceOrientation,你必須做到以下幾點:

在其中要添加一個視圖控制器的viewDidLoad方法來進行旋轉:

self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

加入旋轉控制器要在其中添加視圖此委託方法:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    //NSLog(@"didRotateFromInterfaceOrientation"); 


    if((fromInterfaceOrientation == UIInterfaceOrientationPortrait) || 
     (fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)) 
    {  

     YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    [mainDelegate.tabBarController.view addSubview: viewToBeRotated]; 

    [viewToBeRotated setHidden:NO]; 

    return; 

} 

if(fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
{ 
    [viewToBeRotated removeFromSuperview]; 
    [self.view setHidden:NO]; 
} 

}

您可能還需要添加下面的方法:

- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
               duration:(NSTimeInterval)duration { 

if (toInterfaceOrientation == UIInterfaceOrientationPortrait) 
{ 
    //self.view = self.portrait; 
    YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    [viewToBeRotated removeFromSuperview]; 
    mainDelegate.tabBarController.view.transform = CGAffineTransformIdentity; 
    mainDelegate.tabBarController.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0)); 
    mainDelegate.tabBarController.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0); 

} 
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) 
{ 
    YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    [mainDelegate.tabBarController.view addSubview: viewToBeRotated]; 
    mainDelegate.tabBarController.view.transform = CGAffineTransformIdentity; 
    mainDelegate.tabBarController.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90)); 
    mainDelegate.tabBarController.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0); 
} 
else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
{ 


//self.view = self.portrait; 
     YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate]; 
     [viewToBeRotated removeFromSuperview]; 
     mainDelegate.tabBarController.view.transform = CGAffineTransformIdentity; 
     mainDelegate.tabBarController.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180)); 
     mainDelegate.tabBarController.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0); 
    } 
    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
     YourAppDelegate *mainDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate]; 
     [mainDelegate.tabBarController.view addSubview: viewToBeRotated]; 
     mainDelegate.tabBarController.view.transform = CGAffineTransformIdentity; 
     mainDelegate.tabBarController.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90)); 
     mainDelegate.tabBarController.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0); 
    } 
} 

你也可能想看看

Rotating the iPhone, and instantiating a new UIViewController

TabBarController and navigationControllers in landscape mode

+0

實際上,我換出兩種觀點。我嘗試設置autoResizingMask,但它仍然不起作用。您建議的其他兩種方法應該由my - willAnimateFirstHalfOfRotationToInterfaceOrientation方法處理。它根據方向交換self.view。這個方法甚至都不會被調用。 – Rob 2009-06-15 23:23:42

0

子類的UITabBarController和重寫此方法曾工作:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 

我認爲我真正的問題是XCode沒有編譯更新的代碼。我做了建立清潔,觸摸並重新啓動XCode和模擬器,並最終採取了變化。

+0

這讓所有的視圖控制器都可以旋轉,我想。 – FelixLam 2009-11-17 01:03:45

1

您可以使用一個子類的UITabBarController(或使用加法),要求所選擇的navigationController爲visibleViewController的旋轉請求響應:

@implementation RotatingTabBarController 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
     if([self.selectedViewController isKindOfClass:[UINavigationController class]]){ 
      return [[(UINavigationController*)self.selectedViewController visibleViewController] shouldAutorotateToInterfaceOrientation:interfaceOrientation]; 
     } else { 
      return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation]; 
     } 
} 
@end 
+0

oops,現在只看到這是一個老問題 – FelixLam 2009-11-17 01:09:08

相關問題