如何在WebView中隐藏滚动条?
我切换到 WKWebView ,因为不再建议Apple使用 UIWebView .
I switched to WKWebView because UIWebView is no longer recommended to be used by Apple.
使用以下代码从WebView的容器中加载HTML文件:
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)
效果很好.
但是当我使用WKWebView时,会显示使用UIWebView时隐藏的滚动条.
But the scrollbar that was hidden while using UIWebView gets displayed when I'm using WKWebView.
使用此css样式属性也不起作用(但使用UIWebView可以按预期工作):
Using this css-style attributes is also not working (but using UIWebView it works as expected):
<style>
::-webkit-scrollbar {
display: none!important;
}
</style>
我将iPhone用作运行iOS 11.2的真实设备(不在模拟器中)
I'm using the iPhone as real device (not in simulator) running iOS 11.2.
还尝试使用此Swift代码:
Also tried using this Swift code:
webview.scrollView.showsHorizontalScrollIndicator = false
webview.scrollView.showsVerticalScrollIndicator = false
这根本不起作用.
任何帮助隐藏滚动条的方法将不胜感激.预先感谢!
Any help to hide the scrollbar would be appreciated. Thanks in advance!
您可以为此使用UIScrollViewDelegate.
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
}
}