簡體   English   中英

UITabBar 在旋轉時更改所有視圖

[英]UITabBar change all views on rotate

我有一個帶有多個視圖的標簽欄 controller。 每個視圖都有一個工具欄。 我添加了將背景圖像應用到工具欄的代碼。

我還在 viewdidLoad 中添加了代碼,以便在設備旋轉時觸發每個視圖,這樣我就可以為橫向模式應用不同的背景圖像:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

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

如果我運行應用程序並旋轉,第一個視圖可以工作,但隨后轉到其他選項卡會導致 didRotate 方法不觸發,因為設備已經旋轉。

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

您應該做的是檢查viewWillAppear:上的interfaceOrientation : 並在需要時重新布局 UI。

您不一定需要通知。 您可以使用兩種方法完成此操作

- (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;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM