11

所以这里是我有: 一个UITabBarController处理不同的UIViewControllers。在其中一个UIViewController中,我试图切换当设备旋转到横向时显示的视图。 的重要组成部分,是在横向显示的视图必须采取整个屏幕...隐藏TabBar当旋转iPhone设备到风景

我已经正确实现的方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 

其实我有我的旋转产生正确的,我我观看swaped。 我甚至隐藏状态栏,导航栏和标签栏但我继续有一个空格在屏幕的底部,这是TabBar的地方...

因此,我假设设置隐藏属性的tabBar不足以在整个屏幕上显示视图。我认为在TabBarController或甚至MainWindow中有一些东西需要说“我现在不需要TabBarController”。但我不明白如何正确解决这个问题。

如果有人遇到过这个问题,我将不胜感激。

谢谢, 萨米人。

回答

33

这对我有效。


- (void)viewDidLoad { 
    [super viewDidLoad];  
    previousRect = self.view.frame; 
} 

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

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration; 
{ 
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {  
     [self.navigationController setNavigationBarHidden:TRUE animated:FALSE]; 
     [[UIApplication sharedApplication] setStatusBarHidden:TRUE animated:FALSE]; 
    } 
    else 
    { 
     [self.navigationController setNavigationBarHidden:FALSE animated:FALSE]; 
     [[UIApplication sharedApplication] setStatusBarHidden:FALSE animated:FALSE]; 
    } 
} 

-(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 = previousRect;  
      tabBar.hidden = FALSE; 
     } 
    } 
} 

+0

你救了我的一天..谢谢男人... – Krishnabhadra 2011-03-31 07:22:07

+0

+1也适合我! – 2011-05-11 14:06:51

+0

请标记为答案... :)它的工作原理! ) – 2013-12-09 03:34:46

0

此代码工作正常,但是当我辞退这是模态呈现一个UIViewController,我的观点是状态栏下20像素。 我的看法是在navigationcontroller里面,所以我不会在旋转之前隐藏它。

1
  • 上述解决方案为我工作也只是为iOS6的和上面的一些小变化:
  • 在iOS6的删除行:“previousRect = self.view.frame;”
  • 也替换“动画” with“withAnimation:”
  • 并从底部删除“transView.frame = previousRect;”(在else函数中)
  • 它适用于我这种方式。并非常感谢用户UB。
0

我需要标签栏进入全屏模式在横向视图,我试图建议以上使用

transView.frame = CGRectMake(0, 0, 480, 320);

这原来是一个哈克溶液和提出了许多问题,例如该方法隐藏并重新显示状态栏(在退出纵向视图后重新显示时,视图会与状态栏重叠)。我不会推荐这个。最后,对我来说,完美的工作是推送一个包含横向视图的新视图控制器,并使用委托来重用原始VC的功能。

-1

如果你有你的UITabBarController然后把一个UINavigationController里面,那么你可以使用hidesBottomBarWhenPushed(有点欺骗)来做到这一点。

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration]; 

    if (toInterfaceOrientation == UIInterfaceOrientationPortrait) { 
     self.hidesBottomBarWhenPushed = NO; 
     self.navigationController.viewControllers = self.navigationController.viewControllers; 
     [self transitionToGridLayout]; 
    } 
    else { 
     self.hidesBottomBarWhenPushed = YES; 
     self.navigationController.viewControllers = self.navigationController.viewControllers; 
     [self transitionToCoverflowLayout]; 
    } 
} 

的诀窍是,这样的hidesBottomBarWhenPushed标志拿起把你的视图控制器。您可以使用以下内容。

self.navigationController.viewControllers = self.navigationController.viewControllers; 
0

这种方法为我工作:

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

    UIView *parentView = self.tabBarController.view; 
    CGRect frame = parentView.frame; 
    CGFloat windowHeight = parentView.window.frame.size.height; 

    switch (toInterfaceOrientation) { 
     case UIInterfaceOrientationLandscapeLeft: 
     case UIInterfaceOrientationLandscapeRight: 
      CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height; 
      frame.size.height = windowHeight + tabBarHeight; 
      break; 
     default: 
      frame.size.height = windowHeight; 
      break; 
    } 

    [UIView animateWithDuration:duration animations:^{ 
     parentView.frame = frame; 
    }]; 
} 

(在iOS8上只测试。)

0

子类的TabBarController并在需要时隐藏的TabBar:

class tabBarVC: UITabBarController { 

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { 
     if size.height < size.width { 
      self.tabBar.hidden = true 
     } else { 
      self.tabBar.hidden = false 
     } 
    } 

} 
0

也许你想用这个

- (void)willAnimateRotationToInterfaceOrientation:UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration]; 

    __block UIView *weakTabBar = [self.tabBarController.view.subviews objectAtIndex:1]; 
    weakTabBar.alpha = 0; 
    [UIView animateWithDuration:duration 
          delay:0 
         options:UIViewAnimationOptionCurveEaseIn // slow at the beggining 
        animations:^{ 
         weakTabBar.alpha = 1; 
        } 
        completion:^(BOOL finished) { 
         weakTabBar.alpha = 1; 
        }]; 
    } 

}

这不隐藏标签栏,但使得旋转动画更加平滑。