Hiding the tabbar and removing the space

旧城冷巷雨未停 提交于 2019-12-17 07:15:38

问题


Is there a way to hide tabbar and remove that space left (around 50px) ?

I tried

self.tabBarController?.tabBar.hidden = true
self.extendedLayoutIncludesOpaqueBars = true

No luck. I see blank space.


回答1:


If you're still seeing a black stripe under your hidden tab bar, have you tried to select Extend Edges Under Opaque Bars here?

Make also sure that Under Bottom Bars is still selected. Hope it helps!




回答2:


Swift 3:

extension UITabBarController {
    func setTabBarVisible(visible:Bool, duration: TimeInterval, animated:Bool) {
        if (tabBarIsVisible() == visible) { return }
        let frame = self.tabBar.frame
        let height = frame.size.height
        let offsetY = (visible ? -height : height)

        // animation
        UIViewPropertyAnimator(duration: duration, curve: .linear) {
            self.tabBar.frame.offsetBy(dx:0, dy:offsetY)
            self.view.frame = CGRect(x:0,y:0,width: self.view.frame.width, height: self.view.frame.height + offsetY)
            self.view.setNeedsDisplay()
            self.view.layoutIfNeeded()
        }.startAnimation()
    }

    func tabBarIsVisible() ->Bool {
        return self.tabBar.frame.origin.y < UIScreen.main.bounds.height
    }
}

To use (if for example self is a UITabBarController):

self.setTabBarVisible(visible: false, duration: 0.3, animated: true)

Swift 2.x:

extension UITabBarController {
    func setTabBarVisible(visible:Bool, duration: NSTimeInterval, animated:Bool) {
        if (tabBarIsVisible() == visible) { return }
        let frame = self.tabBar.frame
        let height = frame.size.height
        let offsetY = (visible ? -height : height)

        // animation
        UIView.animateWithDuration(animated ? duration : 0.0) {
            self.tabBar.frame = CGRectOffset(frame, 0, offsetY)
            self.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height + offsetY)
            self.view.setNeedsDisplay()
            self.view.layoutIfNeeded()
        }
    }

    func tabBarIsVisible() ->Bool {
        return self.tabBar.frame.origin.y < UIScreen.mainScreen().bounds.height
    }
}

To use:

self.tabBarController?.setTabBarVisible(visible: false, duration: 0.3, animated: true)



回答3:


After saw your screenshot in comment. I think you can try to set hidesBottomBarWhenPushed to true.

hidesBottomBarWhenPushed = true

Or storyboard.

It will hide bottom bar automatically when you pushed to another view controller, and appear it again when you go back.




回答4:


Programmatically, add this to the next view controller for swift 4.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    tabBarController?.tabBar.isHidden = true
    edgesForExtendedLayout = UIRectEdge.bottom
    extendedLayoutIncludesOpaqueBars = true
}

And add a background color




回答5:


NOTE - This solution is to just to remove white space left after hiding tab bar.

For hiding tab bar best solution is - @Michael Campsall answer here

The simplest solution to this is to change your view's(in my case its tableView) bottom constraints, instead of giving bottom constraints with BottomLayoutGuide give it with superview. Screenshots attached for reference.

Constraints shown in below screenshots creates the problem, change it according to next screenshot.

Actual constraints to remove white space should be according to this(below) screenshot.




回答6:


The third answer on this question works for me in the following way:

The code on my view controller

@IBAction func buttonPressed(sender: AnyObject) {

    setTabBarVisible(!tabBarIsVisible(), animated: true)

}

func setTabBarVisible(visible: Bool, animated: Bool) {
    // hide tab bar
    let frame = self.tabBarController?.tabBar.frame
    let height = frame?.size.height
    var offsetY = (visible ? -height! : height)
    print ("offsetY = \(offsetY)")

    // zero duration means no animation
    let duration:NSTimeInterval = (animated ? 0.3 : 0.0)

    // animate tabBar
    if frame != nil {
        UIView.animateWithDuration(duration) {
            self.tabBarController?.tabBar.frame = CGRectOffset(frame!, 0, offsetY!)
            self.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height + offsetY!)
            self.view.setNeedsDisplay()
            self.view.layoutIfNeeded()
            return
        }
    }
}



func tabBarIsVisible() -> Bool {
    return self.tabBarController?.tabBar.frame.origin.y < UIScreen.mainScreen().bounds.height
}

In storyboard:

The view controller main view background color is black color:

Then you could have another view inside (background color white), constrained trailing and leading space to superview and top and bottom space to the layout guide.

And the result is:




回答7:


For those that like to do everything programmatically, add this line to the init method of a ViewController that shouldn't have the tabBar:

hidesBottomBarWhenPushed = true



回答8:


My preferred way to do that is using a wrapping controller. If I want to hide the tab bar, I just increase the height of the tab bar controller, thus effectively the tab bar is moved out of the screen.

With this solution you don't need to hack tab bar frame and you don't depend on navigation controller push animation:

import UIKit

class ViewController: UIViewController {
    let tabController: UITabBarController = {
        let tabController = UITabBarController()
        // setup your tabbar controller here

        return tabController;
    }()

    var tabbarHidden = false {
        didSet {
            var frame = self.view.bounds;

            if (tabbarHidden) {
                frame.size.height += self.tabController.tabBar.bounds.size.height;
            }

            self.tabController.view.frame = frame;
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // add the tab controller as child controller
        addChildViewController(self.tabController)
        self.tabController.view.frame = self.view.bounds
        self.tabController.view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
        self.view.addSubview(self.tabController.view)
        self.tabController.didMoveToParentViewController(self)

        // for debugging
        let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(switchTabbar))
        self.tabController.view.addGestureRecognizer(tapRecognizer)
    }

    override func childViewControllerForStatusBarStyle() -> UIViewController? {
        return self.tabController
    }

    override func childViewControllerForStatusBarHidden() -> UIViewController? {
        return self.tabController
    }

    func switchTabbar() {
        UIView.animateWithDuration(0.3) {
            self.tabbarHidden = !self.tabbarHidden
        }
    }
}




回答9:


Yes. You can hide your tab bar when you push to view controller. You can show tab bar in your home. You can hide your tab bar when you push to next View controller.

See the Hide Botton Bar on Push following image and set in all viewcontrollers where you dont want tab bar.

Hope it helps..




回答10:


I was facing the same issue and root cause was BOTTOM CONSTRAINT

Make sure you set the bottom constraint of your bottom most view in the main view hierarchy with SUPERVIEW, NOT "SAFE AREA"

Hope this helps someone..




回答11:


You can refer this link - iOS/Swift - Hide/Show UITabBarController when scrolling down/up. For better Result Do not forget to add this line of code in your viewdidLoad() for Black screen removal after hiding Tab bar.

if #available(iOS 11.0, *) {
        self.myScroll.contentInsetAdjustmentBehavior = .never
    }



回答12:


Sometimes that easiest way is just to add a view that uses the UIScreen bounds.

let whiteView = UIView()
    whiteView.backgroundColor = .white
    view.addSubview(whiteView)
    whiteView.translatesAutoresizingMaskIntoConstraints = false
    whiteView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    whiteView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    whiteView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    whiteView.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.height).isActive = true

Cause sometimes the view edges extends beyond the nav bar giving you new problems if you extend the view layout.




回答13:


This code works on iOS 10, 11, and iPhone X (including simulators) to show/hide the tabBar. I created it several years (iOS 7 time frame?) and it has worked reliably since that time.

It works great on iPhone X as long as content content in your childViewControllers (in tabs) is pinned to topLayoutGuide, bottomLayoutGuide or SafeArea and not the main views walls. Then it all just works. Enjoy!

@interface UITabBarController (HideTabBar)
@property (nonatomic, getter=isTabBarHidden) BOOL tabBarHidden;
-(void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated;
@end

@implementation UITabBarController (HideTabBar)
-(BOOL)isTabBarHidden
{
    CGRect viewFrame = self.view.frame;
    CGRect tabBarFrame = self.tabBar.frame;
    return tabBarFrame.origin.y >= viewFrame.size.height;
}

-(void)setTabBarHidden:(BOOL)hidden
{
    [self setTabBarHidden:hidden animated:NO];
}

-(void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated
{
    BOOL isHidden = self.tabBarHidden;    
    if(hidden == isHidden)return;

    UIView *transitionView = [[[self.view.subviews reverseObjectEnumerator] allObjects] lastObject];
    if(transitionView == nil) {
        NSLog(@"UITabBarCategory can't get the container view");
        return;
    }    
    CGRect viewFrame = self.view.bounds;
    CGRect tabBarFrame = self.tabBar.frame;
    CGRect containerFrame = transitionView.frame;
    CGRect selectedVCFrame = containerFrame;

    tabBarFrame.origin.y = viewFrame.size.height - (hidden ? 0 : tabBarFrame.size.height);
    containerFrame.size.height = viewFrame.size.height - (hidden ? 0 : tabBarFrame.size.height);
    if([self.moreNavigationController.viewControllers containsObject:self.selectedViewController]) {
        selectedVCFrame = self.selectedViewController.view.frame;
        selectedVCFrame.size.height += hidden ? tabBarFrame.size.height : -tabBarFrame.size.height;
    }
    self.selectedViewController.view.frame = selectedVCFrame;

    [UIView animateWithDuration:.5 animations:^{
        self.tabBar.frame = tabBarFrame;
        transitionView.frame = containerFrame;
        [self.selectedViewController.view setNeedsLayout];
    }];
}
@end

Usage - I call it in the viewController on rotation events like so:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];

    // Hide TabBar on iPhone, iPod Touch
    if([UIDevice currentDevice].userInterfaceIdiom != UIUserInterfaceIdiomPad) {
        if(_startDateEditor.editing) return;
        if(fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || fromInterfaceOrientation == UIInterfaceOrientationPortrait)
            [self.tabBarController setTabBarHidden:YES animated:YES];
        else
            [self.tabBarController setTabBarHidden:NO animated:YES];
    }
}



回答14:


For me in iOS 13 I had to display image in cell with full screen, I had collection view with trailing, leading, top, bottom constraint. I removed all constraint. set collection view frame to UIScreen.main.bounds. then return sizeForItemAt as collection frame size.



来源:https://stackoverflow.com/questions/37040313/hiding-the-tabbar-and-removing-the-space

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!