問題描述
我對 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!