How to hide scrollbar in WebView?

二次信任 提交于 2020-01-14 06:45:34

问题


I switched to WKWebView because UIWebView is no longer recommended to be used by Apple.

Loading the HTML File from the Container in WebView using this code:

let webview = myWKWebViewClass.webview(for: Bundle.main.filename)
webview.scrollView.isScrollEnabled = false
myContainerView.addSubview(webview)

works quite fine.

But the scrollbar that was hidden while using UIWebView gets displayed when I'm using WKWebView.

Using this css-style attributes is also not working (but using UIWebView it works as expected):

 <style>
    ::-webkit-scrollbar {
         display: none!important;
    }
 </style>

EDIT: I'm using the iPhone as real device (not in simulator) running iOS 11.2.

Also tried using this Swift code:

webview.scrollView.showsHorizontalScrollIndicator = false
webview.scrollView.showsVerticalScrollIndicator = false

It's not working at all.

Any help to hide the scrollbar would be appreciated. Thanks in advance!


回答1:


You can use UIScrollViewDelegate for that.

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



回答2:


You can use visibility:none instead of display



来源:https://stackoverflow.com/questions/48971789/how-to-hide-scrollbar-in-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!