hiding TabBar when rotating iPhone device to landscape
Asked Answered
T

8

11

So here is what i have: A UITabBarController that handles different UIViewControllers. In one of the UIViewController i am trying to switch the view being displayed when the device rotates to landscape. the important part is that the view displayed in landscape MUST take the whole screen...

I have correctly implemented the methods :

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

In fact i do have my rotation occurring correctly, and I my views swaped. i even hide status bar, nav bar and Tab bar BUT i keep having a blank space at the bottom of the screen which is the place of the TabBar...

So i am assuming that setting the hidden property of the tabBar is not enough in order to have the view on the whole screen. I think there is some stuff to do within the TabBarController or even the MainWindow to say somehting like "i don't need TabBarController now". But i do not see how to get around this problem properly.

If anyone has been around this issue, i would appreciate some help.

thank you, Sami.

Tierratiersten answered 22/7, 2009 at 10:12 Comment(1)
https://mcmap.net/q/1017914/-hide-uitabbar-in-landscape Xcode12 and year 2020 answerCurr
D
33

This worked for me.


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

Deettadeeyn answered 12/10, 2009 at 13:37 Comment(1)
Use YES/NO instead of TRUE/FALSE. Although it works, it is not the convention.Counterreply
P
1
  • The solution above worked for me too just some little changes for iOS6 and above:
  • in iOS6 remove the line: " previousRect = self.view.frame; "
  • also replace " animated: " with " withAnimation: "
  • and remove " transView.frame = previousRect; " from the bottom (in else function)
  • It works for me this way. And a big thanks to user UB.
Petitioner answered 19/11, 2012 at 23:10 Comment(0)
B
0

This code works fine but when i dismiss a uiviewcontroller which is presented modally, my view is under the status bar by 20 pixel. My view is inside a navigationcontroller so i do not hide it before rotation.

Burdelle answered 8/5, 2012 at 15:16 Comment(1)
i figured out that the problem is related with the navigation bar and only in IOS 5.If you experience similar problems just hide and show your navigation bar consequently in viewwillappear.Burdelle
A
0

I needed tab bar to go into full screen mode in landscape view and I tried the approach suggested above using

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

This turned out to be a hacky solution and posed many problems such as with hiding and re-displaying the status bar (the view would overlap with the status bar when it is re-displayed after exiting portrait view). I would not recommend this. What worked for me perfectly in the end was pushing a new view controller containing the landscape view and using delegation to reuse the functionality of the original VC.

Allegro answered 23/11, 2012 at 5:32 Comment(0)
H
0

This approach is working for me:

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

(Only tested in iOS8.)

Harbor answered 30/11, 2014 at 18:49 Comment(3)
I have this code running in a production app just fine. What are you seeing? Which version of iOS 8?Harbor
Tried iOS 8.2 simulator the tab bar is shown during rotation. Btw whats the logic: when rotating to landscape tab bar frame height is increased by window height?Backache
Haven't tried it in the simulator (am using frameworks that aren't built for that platform), but works just fine on devices in iOS 8.1/8.2/8.3. The logic is to increase the window height by the tab bar height when rotating to landscape orientation so that the window is taller than the device screen enough such that the tab bar is rendered offscreen (and undo the height change when rotating back to portrait).Harbor
M
0

Subclass your TabBarController and hide the TabBar when needed:

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

}
Maneuver answered 21/4, 2015 at 12:28 Comment(0)
B
0

Maybe you want to use this

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

}

This doesn't hide the tab bar but makes the rotate animation smoother.

Backache answered 24/4, 2015 at 19:25 Comment(0)
G
-1

If you have your UITabBarController then put a UINavigationController inside it then you can use hidesBottomBarWhenPushed (with a bit of trickery) to do this.

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

The trick is to push your view controller so that the hidesBottomBarWhenPushed flag is picked up. You can use following.

self.navigationController.viewControllers = self.navigationController.viewControllers;
Greco answered 3/7, 2013 at 16:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.