How to hide the navigation bar and toolbar as scro

2019-01-16 12:02发布

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?

6条回答
【Aperson】
2楼-- · 2019-01-16 12:14

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;
    }
}
查看更多
乱世女痞
3楼-- · 2019-01-16 12:16

Try this simple approach: 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

enter image description here

Note: If you passing any data from this VC to another VC that embedded with navigationController.You may need to unhide the NavigationBar.

查看更多
欢心
4楼-- · 2019-01-16 12:16

you can try self.navigationController?.hidesBarsOnTap = true in viewDidAppear also you can use hide on swipe.

查看更多
放荡不羁爱自由
5楼-- · 2019-01-16 12:17

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

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
    }

    ...
}
查看更多
兄弟一词,经得起流年.
6楼-- · 2019-01-16 12:36

Easily to do this:

navigationController?.hidesBarsOnSwipe = true
查看更多
Melony?
7楼-- · 2019-01-16 12:36

Thanks everyone, the way I went with was using AMScrollingController.

https://github.com/andreamazz/AMScrollingNavbar

It's updated for Swift 3

查看更多
登录 后发表回答