iOS/Swift - Hide/Show UITabBarController when scrolling down/up(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
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01