隐藏tabbar并删除空格

问题描述 投票:33回答:13

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

我试过了

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

没运气。我看到空白。

ios swift uitabbar
13个回答
54
投票

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

enter image description here

还要确保仍然选择Under Bottom Bars。希望能帮助到你!


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

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


0
投票

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

只要childViewControllers(在选项卡中)中的内容内容固定到topLayoutGuidebottomLayoutGuide或SafeArea而不是主视图墙,它在iPhone X上运行良好。然后一切正常。请享用!

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

0
投票

我面临同样的问题,根本原因是BOTTOM CONSTRAINT

确保使用SUPERVIEW设置主视图层次结构中最底部视图的底部约束,而不是“安全区域”

希望这有助于某人..


0
投票

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

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

33
投票

斯威夫特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)

24
投票

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

hidesBottomBarWhenPushed = true

或故事板。

enter image description here

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


8
投票

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

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

并添加背景颜色


5
投票

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

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

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

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

Change constraints shown in this screenshot according to below screenshot

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

enter image description here


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


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


1
投票

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

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

enter image description here

希望能帮助到你..


1
投票

对于那些喜欢以编程方式完成所有操作的人,将此行添加到不应该有tabBar的initViewController方法中:

hidesBottomBarWhenPushed = true
© www.soinside.com 2019 - 2024. All rights reserved.