(Xcode / Swift) 向下滚动 UIWebView 时如何隐藏工具栏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31823857/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:37:30  来源:igfitidea点击:

(Xcode / Swift) How to hide toolbar when scrolling down UIWebView?

xcodeswiftuiwebviewtoolbaruitoolbar

提问by user3796209

I have a UIWebViewin my ViewControllerand a navigation controller embedded in my ViewController. In my navigation controller, I selected "Show Toolbar"and "Hide Bars on Swipe"but the Toolbar doesn't hide. It only works when "Show Navigation Bar"is selected with the Toolbar.

我有一个UIWebView在我的ViewController并嵌入我的导航控制器的ViewController。在我的导航控制器中,我选择了“显示工具栏”“滑动时隐藏栏”,但工具栏没有隐藏。它仅在使用工具栏选择“显示导航栏”时有效

Is there anyway to have the Toolbarhide on swipe when scrolling down the UIWebView?

无论如何,在向下滚动UIWebView时是否可以在滑动时隐藏工具栏

Thank you in advanced.

先谢谢了。

回答by Dharmesh Kheni

You can use UIScrollViewDelegatefor that.

你可以用UIScrollViewDelegate它。

Here is example code for hide navigation bar and tool bar with scroll:

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

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