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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)