如何在WebView中隐藏滚动条? [英] How to hide scrollbar in WebView?

查看:858
本文介绍了如何在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
    }
}

这篇关于如何在WebView中隐藏滚动条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆