在 UIWebView 上覆盖一个刷新按钮

Overlay a refresh button over a UIWebView

我正在尝试在我的应用程序的右上角添加一个按钮,该按钮将重新加载我的 webView。我试图遵循其他答案 and ,但没有任何运气让按钮出现。

我的 ViewController class 中有以下代码:

override func viewDidLoad() {
    super.viewDidLoad()
    let webView:UIWebView = UIWebView(frame: CGRectMake(30, 20, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
    webView.scalesPageToFit = true
    webView.multipleTouchEnabled = true
    webView.backgroundColor = UIColor.whiteColor()
    webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.example.com")!))
    self.view.addSubview(webView)
}

我想覆盖按钮。我如何添加按钮以在按下时重新加载 webView

您可以简单地将按钮添加到您的网络视图层次结构中,例如

    let buttonFrame = CGRect(x: 50, y: 150, width: 50, height: 50)
    let button = UIButton(frame: buttonFrame)
    button.backgroundColor = UIColor.greenColor()
    webView.scrollView.addSubview(button) // in that way button will be scroling with webView.

或在 webView 上方添加按钮,例如

        let buttonFrame = CGRect(x: 50, y: 150, width: 50, height: 50)
        let button = UIButton(frame: buttonFrame)
        button.backgroundColor = UIColor.greenColor()
        insertSubview(button, aboveSubview: webView)