如何在滚动iOS上显示导航栏

问题描述 投票:1回答:2

我找到了很多关于如何在滚动上隐藏导航栏的资源,但是我想在开始时隐藏导航栏,然后在开始滚动时出现。喜欢这个来自Design + Code app的动画:https://imgur.com/a/SqDRD

ios swift uinavigationbar
2个回答
1
投票

你可以使用UIScrollViewDelegate

以下是隐藏导航栏和带滚动的工具栏的示例代码:

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var toolBar: UIToolbar!
    @IBOutlet weak var webV: UIWebView!
    var lastOffsetY :CGFloat = 0
    override func viewDidLoad() {
        super.viewDidLoad()

        webV.scrollView.delegate = self
        let url = "http://apple.com"
        let requestURL = NSURL(string:url)
        let request = NSURLRequest(URL: requestURL!)
        webV.loadRequest(request)
    }

    //Delegate Methods
    func scrollViewWillBeginDragging(scrollView: UIScrollView){
        lastOffsetY = scrollView.contentOffset.y
    }

    func scrollViewWillBeginDecelerating(scrollView: UIScrollView){

        let hide = scrollView.contentOffset.y > self.lastOffsetY
        self.navigationController?.setNavigationBarHidden(hide, animated: true)
        toolBar.hidden = hide
    }
}

1
投票

使用willBeginDraggingdidEndDragging,你可以完成你想要的。这里有一个简化版本,你可能需要稍微修改它以获得你想要的效果,但这是一个起点。

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.setNavigationBarHidden(true, animated: false)

    }
}

extension ViewController: UIScrollViewDelegate {
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        navigationController?.setNavigationBarHidden(false, animated: true)
    }

    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        navigationController?.setNavigationBarHidden(true, animated: true)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.