iOS / Swift – 向下/向上滚动时隐藏/显示UITabBarController

我是iOS开发的新手.现在我正在尝试隐藏我的标签栏,当我向下滚动时,向上滚动标签栏应该出现.我想像导航栏一样动画这个动画.对于导航栏,我只需单击“属性”检查器中的选项.我看到了工具栏的一些示例,但我不能将其用于tabbar.

self.tabBarController?.tabBar.hidden = true只是隐藏我的tabbar,但它不像导航控制器那样动画.

解决方法

这是我实际在生产应用程序中使用的代码.

它在Swift中,它也更新了UITabBar.hidden var.

func scrollViewWillBeginDragging(scrollView: UIScrollView) {
    if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{
        changeTabBar(hidden: true,animated: true)
    }
    else{
        changeTabBar(hidden: false,animated: true)
    }
}

您还可以使用其他回调方法

func scrollViewDidScroll(scrollView: UIScrollView) {
    ...
}

但是如果你选择这样,那么你必须处理多个实际隐藏tabBar的辅助方法调用.

然后你需要添加这个方法来动画tabBar的隐藏/显示.

func changeTabBar(hidden:Bool,animated: Bool){
    var tabBar = self.tabBarController?.tabBar
    if tabBar!.hidden == hidden{ return }
    let frame = tabBar?.frame
    let offset = (hidden ? (frame?.size.height)! : -(frame?.size.height)!)
    let duration:NSTimeInterval = (animated ? 0.5 : 0.0)
    tabBar?.hidden = false
    if frame != nil
    {
        UIView.animateWithDuration(duration,animations: {tabBar!.frame = CGRectOffset(frame!,offset)},completion: {
                println($0)
                if $0 {tabBar?.hidden = hidden}
        })
    }
}

更新Swift 4

func changeTabBar(hidden:Bool,animated: Bool){
    guard let tabBar = self.tabBarController?.tabBar else { return; }
    if tabBar.isHidden == hidden{ return }
    let frame = tabBar.frame
    let offset = hidden ? frame.size.height : -frame.size.height
    let duration:TimeInterval = (animated ? 0.5 : 0.0)
    tabBar.isHidden = false

    UIView.animate(withDuration: duration,animations: {
        tabBar.frame = frame.offsetBy(dx: 0,dy: offset)
    },completion: { (true) in
        tabBar.isHidden = hidden
    })
}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...