隐藏tabbar并删除空格

时间:2016-05-05 00:24:15

标签: ios swift uitabbar

有没有办法隐藏tabbar并删除剩下的空间(大约50px)?

我试过

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

没有运气。我看到空白。

14 个答案:

答案 0 :(得分:53)

如果您仍然在隐藏的标签栏下方看到黑色条纹,您是否尝试在此处选择在不透明栏下延伸边缘

enter image description here

确保仍然选择 Under Bottom Bars 。希望它有所帮助!

答案 1 :(得分:31)

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

要使用(例如selfUITabBarController):

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

使用:

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

答案 2 :(得分:24)

在评论中看到你的截图。我想你可以尝试将hidesBottomBarWhenPushed设置为true。

hidesBottomBarWhenPushed = true

或故事板。

Here is riak configuration example for ejabberd modules

当您推送到另一个视图控制器时,它会自动隐藏底栏,并在您返回时再次显示。

答案 3 :(得分:6)

以编程方式将其添加到swift 4的下一个视图控制器。

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

添加背景颜色

答案 4 :(得分:5)

注意 - 此解决方案仅用于删除隐藏标签栏后留下的空白区域。

隐藏标签栏的最佳解决方案是 - @Michael Campsall answer here

最简单的解决方案是更改您的视图(在我的情况下是其tableView)底部约束,而不是使用BottomLayoutGuide给出底部约束,并使用superview。附上截图以供参考。

以下屏幕截图中显示的约束会产生问题,请根据下一屏幕截图进行更改。

Change constraints shown in this screenshot according to below screenshot

删除空格的实际约束应该根据此(下面)截图。

enter image description here

答案 5 :(得分:3)

this question的第三个答案以下列方式为我工作:

我的视图控制器上的代码

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

在故事板中:

视图控制器主视图背景颜色为黑色: enter image description here

然后你可以在里面有另一个视图(背景颜色为白色),约束拖尾和前导空间到superview以及布局指南的顶部和底部空间。

enter image description here

结果是:

enter image description here

答案 6 :(得分:2)

我首选的方法是使用包装控制器。如果我想隐藏标签栏,我只需增加标签栏控制器的高度,从而有效地将标签栏移出屏幕。

使用此解决方案,您不需要破解标签栏框架,并且您不依赖于导航控制器推送动画:

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

enter image description here

答案 7 :(得分:1)

是。当您按下查看控制器时,可以隐藏标签栏。您可以在家中显示标签栏。当您按下下一个View控制器时,可以隐藏标签栏。

请参阅下面的图像中的按下隐藏按钮栏,并在所有不需要标签栏的视图控制器中进行设置。

enter image description here

希望有所帮助......

答案 8 :(得分:0)

有时候最简单的方法就是添加一个使用UIScreen边界的视图。

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

如果扩展视图布局,有时视图边缘会延伸到导航栏之外,从而导致出现新问题。

答案 9 :(得分:0)

此代码适用于 iOS 10,11 iPhone X (包括模拟器)显示/隐藏tabBar 。我创建了几年(iOS 7时间框架?),从那时起它已经可靠地工作了。

只要您的childViewControllers(在标签中)中的内容内容固定到topLayoutGuidebottomLayoutGuide或SafeArea并且不<<>,它在 iPhone X 上效果很好/ strong>主要观点墙。然后一切正常。享受!

@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

用法 - 我在viewController中调用旋转事件,如下所示:

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

答案 10 :(得分:0)

我遇到了同样的问题,根本原因是底部约束

请确保使用 SUPERVIEW(不是“安全区域”

在主视图层次结构中设置最底视图的底部约束

希望这对某人有帮助。

答案 11 :(得分:0)

您可以参考此链接-iOS/Swift - Hide/Show UITabBarController when scrolling down/up。为了更好的结果 隐藏标签栏后,不要忘记在您的viewdidLoad()中添加此行代码以删除黑屏。

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

答案 12 :(得分:0)

对于我来说,在iOS 13中,我必须全屏显示单元格中的图像,我的收藏夹视图受trailing, leading, top, bottom约束。我删除了所有约束。将集合视图框架设置为UIScreen.main.bounds。然后返回sizeForItemAt作为收集帧的大小。

答案 13 :(得分:0)

当你想再次显示时,尝试将标签栏设置为半透明,然后再隐藏标签栏设置为false。

它对我有用。

tabBarController?.tabBar.isTranslucent = true