ios 如何在向下滚动时隐藏导航栏和工具栏?Swift(如 myBridge 应用程序)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40667985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to hide the navigation bar and toolbar as scroll down? Swift (like myBridge app)
提问by Lyndon King McKay
I want to hide a toolbar and nav bar as I scroll down a page. And return it as I scroll up. How is this possible?
我想在向下滚动页面时隐藏工具栏和导航栏。并在我向上滚动时返回它。这怎么可能?
How would I go about detecting the drag? Do I use pan gesture or is this down with the scrollview?
我将如何检测阻力?我是使用平移手势还是滚动视图向下?
回答by Joe
Try this simple approach: Tested in Swift 3
试试这个简单的方法:Tested in Swift 3
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if(velocity.y>0) {
//Code will work without the animation block.I am using animation block incase if you want to set any delay to it.
UIView.animate(withDuration: 2.5, delay: 0, options: UIViewAnimationOptions(), animations: {
self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.setToolbarHidden(true, animated: true)
print("Hide")
}, completion: nil)
} else {
UIView.animate(withDuration: 2.5, delay: 0, options: UIViewAnimationOptions(), animations: {
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationController?.setToolbarHidden(false, animated: true)
print("Unhide")
}, completion: nil)
}
}
Output:Updated
输出:Updated
Note:If you passing any data from this VC to another VC that embedded with navigationController
.You may need to unhide
the NavigationBar
.
注意:如果您将此 VC 中的任何数据传递给另一个嵌入了navigationController
.您的VC,您可能需要unhide
将NavigationBar
.
回答by Huu Phong Nguyen
Easily to do this:
轻松做到这一点:
navigationController?.hidesBarsOnSwipe = true
回答by vivek agravat
you can try self.navigationController?.hidesBarsOnTap = true
in viewDidAppear also you can use hide on swipe.
您可以self.navigationController?.hidesBarsOnTap = true
在 viewDidAppear中尝试,也可以在滑动时使用隐藏。
回答by Lyndon King McKay
Thanks everyone, the way I went with was using AMScrollingController.
谢谢大家,我采用的方式是使用 AMScrollingController。
https://github.com/andreamazz/AMScrollingNavbar
https://github.com/andreamazz/AMScrollingNavbar
It's updated for Swift 3
它已针对 Swift 3 更新
回答by rakeshNS
In my opinion the proper way to handle navigation bar in Tableview as follows. This would applicable if we have section header in Tableview.
在我看来,在 Tableview 中处理导航栏的正确方法如下。如果我们在 Tableview 中有节标题,这将适用。
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0 {
navigationController?.setNavigationBarHidden(true, animated: true)
} else {
navigationController?.setNavigationBarHidden(false, animated: true)
}
}
回答by Ahmed Abdallah
Swift 5 Xcode 10.3
斯威夫特 5 Xcode 10.3
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.hidesBarsOnSwipe = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.hidesBarsOnSwipe = false
}
回答by BatyrCan
Here is very good option for that
这是一个很好的选择
Easily hide and show a view controller's navigationBar/tabBar as a user scrolls https://github.com/tristanhimmelman/HidingNavigationBar
当用户滚动https://github.com/tristanhimmelman/HidingNavigationBar 时,轻松隐藏和显示视图控制器的 navigationBar/tabBar
import HidingNavigationBar
class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var hidingNavBarManager: HidingNavigationBarManager?
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
hidingNavBarManager = HidingNavigationBarManager(viewController: self, scrollView: tableView)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
hidingNavBarManager?.viewWillAppear(animated)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
hidingNavBarManager?.viewDidLayoutSubviews()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
hidingNavBarManager?.viewWillDisappear(animated)
}
//// TableView datasoure and delegate
func scrollViewShouldScrollToTop(scrollView: UIScrollView) -> Bool {
hidingNavBarManager?.shouldScrollToTop()
return true
}
...
}
回答by Septronic
I implemented this in my scrollview, as I was using components other than UITableView
or UICollectionView
, not sure if it works for you, but it's working perfectly for me:
我在我的滚动视图中实现了这一点,因为我使用的是UITableView
or以外的组件UICollectionView
,不确定它是否适合你,但它对我来说完美无缺:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let totalTop = (UIApplication.shared.statusBarFrame.size.height ?? 0) + (self.navigationController?.navigationBar.frame.height ?? 0)
let shouldHideNavBar = scrollView.contentOffset.y > -(totalTop - 20) // 20 is an arbitrary number I added to compensate for some of scrolling
navigationController?.setNavigationBarHidden(shouldHideNavBar, animated: true)
}
回答by neha mishra
You can use these lines of code :
您可以使用这些代码行:
- (void)scrollViewDidScroll: (UIScrollView *)scroll {
// UITableView only moves in one direction, y axis
CGFloat currentOffset = scroll.contentOffset.y;
CGFloat maximumOffset = scroll.contentSize.height - scroll.frame.size.height;
// Change 10.0 to adjust the distance from bottom
if (maximumOffset - currentOffset <= 10.0) {
self.navigationController?.hidden = YES;
}
else{
self.navigationController?.hidden = NO;
}
}