2011-05-26 51 views
0

我有一個具有多個視圖的選項卡欄控制器。每個視圖都有一個工具欄。我添加了代碼以將背景圖像應用於工具欄。UITabBar在旋轉時更改所有視圖

我還添加了代碼,在viewDidLoad中,在每個視圖火災時,該設備被旋轉,所以我可以申請風景模式不同的背景圖像:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; 
[notificationCenter addObserver:self selector:@selector(didRotate:) 
name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 

如果我運行該應用並旋轉時,第一個視圖起作用,但隨後轉到其他選項卡會導致didRotate方法無法觸發,因爲設備已經旋轉。

如何在設備旋轉時更新所有視圖?

回答

0

您不一定需要通知。你可以做到這一點使用兩種方法

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation 
           duration:(NSTimeInterval)duration 

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 

例如,爲了使在橫向模式下使用TabBar隱藏而不是在人像,用這個:

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

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation 
           duration:(NSTimeInterval)duration 
{ 
     if (toOrientation == UIInterfaceOrientationLandscapeLeft || 
      toOrientation == UIInterfaceOrientationLandscapeRight) { 
      [[self view] endEditing:YES]; 
      [[self view] addSubview:graphView]; 
      //[[UIApplication sharedApplication] setStatusBarHidden:TRUE withAnimation:FALSE]; 
      [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque]; 
      [self.navigationController setNavigationBarHidden:TRUE animated:TRUE]; 
     }  

} 

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    UIInterfaceOrientation toOrientation = self.interfaceOrientation; 

    if (self.tabBarController.view.subviews.count >= 2) 
    { 
     UIView *transView = [self.tabBarController.view.subviews objectAtIndex:0]; 
     UIView *tabBar = [self.tabBarController.view.subviews objectAtIndex:1]; 

     if(toOrientation == UIInterfaceOrientationLandscapeLeft || 
      toOrientation == UIInterfaceOrientationLandscapeRight) {          
      transView.frame = CGRectMake(0, 0, 480, 320); 
      tabBar.hidden = TRUE; 
     } 
     else 
     {        
      transView.frame = CGRectMake(0, 0, 320, 480);   
      tabBar.hidden = FALSE; 
     } 
    } 
} 
2

你應該做的是檢查interfaceOrientationviewWillAppear:並根據需要重新佈局UI。