iOS/Swift - 向下/向上滚动时隐藏/显示 UITabBarContr

iOS/Swift - Hide/Show UITabBarController when scrolling down/up(iOS/Swift - 向下/向上滚动时隐藏/显示 UITabBarController)

本文介绍了iOS/Swift - 向下/向上滚动时隐藏/显示 UITabBarController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 iOS 开发很陌生.现在,当我向下滚动和向上滚动时,我正试图隐藏我的标签栏.我想让这个动画像导航栏一样.对于导航栏,我只需单击 Attributes Inspector 中的选项.我看到了一些工具栏的例子,但我不能采用它作为标签栏.

I'm quite new to iOS development. Right now i'm trying to hide my tabbar when I scroll down and when scrolling up the tabbar should appear. I would like to have this animated in the same way like the navigation bar. For the navigation bar I simply clicked the option in the Attributes Inspector. I saw some examples for the toolbar, but I cant adopt it the tabbar.

self.tabBarController?.tabBar.hidden = true 只是隐藏了我的标签栏,但它不像导航控制器那样具有动画效果.

self.tabBarController?.tabBar.hidden = true just hides my tabbar, but its not animated like the navigation controller.

推荐答案

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

This is code that i'm actually using in a production app.

它在 Swift 中,它还更新 UITabBar.hidden var.

It's in Swift and it also updates 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)
    }
}

你也可以使用其他回调方法:

You can also use the other callback method:

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

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

but if you choose so, then you must handle multiple calls to the helper method that actually hides the tabBar.

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

And then you need to add this method that animates the hide/show of the 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!, 0, 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/Swift - 向下/向上滚动时隐藏/显示 UITabBarController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:iOS/Swift - 向下/向上滚动时隐藏/显示 UITabBarContr

基础教程推荐