如何向下滚动隐藏导航栏和工具栏? Swift(喜欢myBridge应用程序)

时间:2016-11-18 01:30:29

标签: ios swift uinavigationbar uitoolbar

我想在向下滚动页面时隐藏工具栏和导航栏。当我向上滚动时返回它。这怎么可能?

我如何检测阻力?我是否使用平移手势或者滚动视图是否显示?

10 个答案:

答案 0 :(得分:70)

尝试这种简单的方法:在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)    
      }
   }

输出: Updated

enter image description here

注意:如果您将此VC中的任何数据传递到嵌入navigationController的另一个VC。您可能需要unhide NavigationBar

答案 1 :(得分:16)

轻松做到这一点:

navigationController?.hidesBarsOnSwipe = true

答案 2 :(得分:5)

您可以在viewDidAppear中尝试self.navigationController?.hidesBarsOnTap = true,也可以在滑动时使用隐藏。

答案 3 :(得分:5)

谢谢大家,我采用的方式是使用AMScrollingController。

https://github.com/andreamazz/AMScrollingNavbar

已针对Swift 3进行了更新

答案 4 :(得分:5)

我认为在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)
   }
}

答案 5 :(得分:1)

Swift 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
  }

答案 6 :(得分:0)

我在滚动视图中实现了此功能,因为我使用的是UITableViewUICollectionView以外的组件,不确定它是否对您有用,但对我来说很完美:

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)
}

答案 7 :(得分:0)

快速用户界面

extension UINavigationController {
    override open func viewDidLoad() {
        super.viewDidLoad()
    hidesBarsOnSwipe = true
    // other customizations
    navigationBar.tintColor = .white
 }
}

答案 8 :(得分:-1)

这是

的非常好的选择

在用户滚动时轻松隐藏并显示视图控制器的navigationBar / tabBar https://github.com/tristanhimmelman/HidingNavigationBar

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
    }

    ...
}

答案 9 :(得分:-6)

您可以使用以下代码行:

- (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;
    }
}
相关问题