Ios 隐藏选项卡栏并删除空间

Ios 隐藏选项卡栏并删除空间,ios,swift,uitabbar,Ios,Swift,Uitabbar,有没有办法隐藏tabbar并删除剩下的空间(大约50px) 我试过了 self.tabBarController?.tabBar.hidden = true self.extendedLayoutIncludesOpaqueBars = true 不走运。我看到了空白。关于的第三个答案对我的作用如下: 我的视图控制器上的代码 @IBAction func buttonPressed(sender: AnyObject) { setTabBarVisible(!tabBarIsVisi

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

我试过了

self.tabBarController?.tabBar.hidden = true
self.extendedLayoutIncludesOpaqueBars = true
不走运。我看到了空白。

关于的第三个答案对我的作用如下:

我的视图控制器上的代码

@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
}
@iAction func按钮按下(发件人:AnyObject){
setTabBarVisible(!tabBarIsVisible(),已设置动画:true)
}
func settabbar可见(可见:Bool,动画:Bool){
//隐藏选项卡栏
设frame=self.tabBar控制器?.tabBar.frame
让高度=框架?.size.height
变量offsetY=(可见?-height!:height)
打印(“offsetY=\(offsetY)”)
//零持续时间意味着没有动画
let duration:NSTimeInterval=(动画?0.3:0.0)
//设置选项卡栏的动画
如果帧!=nil{
UIView.animateWithDuration(持续时间){
self.tabBar控制器?.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.layoutifneed()
返回
}
}
}
func tabBarIsVisible()->Bool{
返回self.tabBar控制器?.tabBar.frame.origin.y
在故事板中:

视图控制器主视图背景颜色为黑色:

然后,您可以在内部创建另一个视图(背景色为白色),将尾随和前导空间约束到superview,并将顶部和底部空间约束到布局指南

结果是:


在评论中看到您的屏幕截图后。我想您可以尝试将按下时的
hidesbottombar设置为true

hidesBottomBarWhenPushed = true
或者故事板


当您按下另一个视图控制器时,它将自动隐藏底部栏,当您返回时,它将再次显示。

是。按下“视图控制器”按钮时,可以隐藏选项卡栏。您可以在家中显示选项卡栏。当按下下一个视图控制器时,可以隐藏选项卡栏

请参见下图中的“推”按钮上的隐藏按钮栏,并在不需要选项卡栏的所有ViewController中进行设置


希望有帮助。

如果您在隐藏的选项卡栏下仍看到黑色条纹,您是否尝试在此处选择不透明栏下的“延伸边”


还要确保底部栏下仍处于选中状态。希望有帮助

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

使用此解决方案,您无需破解选项卡栏框架,也无需依赖导航控制器推送动画:

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

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

注意-此解决方案仅用于删除隐藏选项卡栏后留下的空白。

对于隐藏选项卡栏,最好的解决方案是-@Michael Campsall

最简单的解决方案是更改视图(在我的示例中是其tableView)的底部约束,而不是使用BottomLayoutGuide提供底部约束使用superview提供。附上截图供参考

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

删除空白的实际限制应根据以下屏幕截图


对于那些喜欢以编程方式执行所有操作的人,将这一行添加到
ViewController
init
方法中,该方法不应具有选项卡栏:

hidesBottomBarWhenPushed = true

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

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

添加背景色有时候最简单的方法就是添加一个使用UI屏幕边界的视图

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

原因有时视图边缘会延伸到导航栏之外,如果您延伸视图布局,则会出现新问题

此代码适用于iOS 10、iOS 11和iPhone X(包括模拟器),以显示/隐藏选项卡栏。我创建了它几年(iOS 7的时间框架?),从那时起,它一直工作可靠

只要您的ChildViewController(选项卡中)中的内容固定在
topLayoutGuide
bottomLayoutGuide
或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];
    }
}

我面临同样的问题,根本原因是底部约束

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

希望这对某人有所帮助。

您可以参考此链接-。为了更好的结果 在隐藏选项卡栏后,请不要忘记在viewdidLoad()中添加这行代码以删除黑屏

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

对于我来说,在iOS 13中,我必须全屏显示单元格中的图像,我有
尾随、前导、顶部、底部
约束的集合视图。我消除了所有的限制。将集合视图框设置为
UIScreen.main.bounds
。然后返回
sizeForItemAt
作为收集帧大小

在隐藏选项卡栏之前,尝试将选项卡栏设置为半透明,如果要再次显示,则再次将选项卡栏设置为false

它对我有用

tabBarController?.tabBar.isTranslucent = true

您可以在删除之前调整选项卡栏显示的视图的大小。@thefreedElement您能给我一个例子吗?怎么样?@ILikeTau他们不会删除隐藏选项卡后留下的空间第二个答案似乎是通过将底部增加49.0像素来实现的。请检查此屏幕截图,正如你所看到的,仍然有黑色的空格,答案中有什么错误??给我一个向下投票的理由??这绝对是最好的,开箱即用,无需在ViewWillIspect/English方法中添加任何内容。以前从来没有注意到那里有财产!性能
if #available(iOS 11.0, *) {
        self.myScroll.contentInsetAdjustmentBehavior = .never
    }
tabBarController?.tabBar.isTranslucent = true