2014-07-23 17 views
1

我目前隱藏標籤欄用這段代碼:隱藏標籤欄的景觀,但在特定情況下(錯誤)再次出現

#pragma mark - Rotation status bar methods 
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) { 
     // only iOS 7 methods, code from http://stackoverflow.com/questions/18525778/status-bar-still-showing 
     [self prefersStatusBarHidden]; 
     [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)]; 
    }else { 
     // iOS 6 code only here...checking if we are now going into landscape mode 
     if((toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)) 
      [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide]; 
     else 
      [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide]; 
    } 
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; 
} 

- (BOOL)prefersStatusBarHidden 
{ 
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 
    if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight){ 
     [self setTabBarVisible:NO animated:NO]; 
     return YES; 
    } 
    else{ 
     [self setTabBarVisible:YES animated:NO]; 
     return NO; //returned when in portrait, or when app is first launching (UIInterfaceOrientationUnknown) 
    } 
} 

#pragma mark - Rotation tab bar methods 
- (void)setTabBarVisible:(BOOL)visible animated:(BOOL)animated { 

    // bail if the current state matches the desired state 
    if ([self tabBarIsVisible] == visible) return; 

    // get a frame calculation ready 
    CGRect frame = self.tabBarController.tabBar.frame; 
    CGFloat height = frame.size.height; 
    CGFloat offsetY = (visible)? -height : height; 

    // zero duration means no animation 
    CGFloat duration = (animated)? 0.3 : 0.0; 

    [UIView animateWithDuration:duration animations:^{ 
     self.tabBarController.tabBar.frame = CGRectOffset(frame, 0, offsetY); 
    }]; 
} 

- (BOOL)tabBarIsVisible { 
    return self.tabBarController.tabBar.frame.origin.y < CGRectGetMaxY(self.view.frame); 
} 

這工作完全,一個案件的例外!上面的代碼位於UiTableViewController(嵌入在導航控制器中)中。當我繼續到另一個viewController(讓說縱向模式),旋轉到橫向,然後點擊後退按鈕,我最終回到我的UiTableViewController ...在橫向模式。但是標籤欄本身會重新出現。

這是什麼原因,我該如何解決這個問題?

PS:手動調用此代碼嘗試並重新隱藏標籤欄已被證明是徒勞的努力大聲笑。

回答

0

添加以下代碼viewWillAppear中修復了這個問題對我來說(我沒有碰任何現有代碼的)

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight){ 
    self.tabBarController.tabBar.hidden = YES; 
} 
else{ 
    self.tabBarController.tabBar.hidden = NO; 
} 

還是不明白,爲什麼標籤欄是重現,但這仍然解決了問題。

相關問題