ios - 在 UIWebView 上覆盖一个刷新按钮

标签 ios swift uiwebview uibutton

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

我的 ViewController 类中有以下代码:

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

最佳答案

您可以简单地将按钮添加到您的 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)

关于ios - 在 UIWebView 上覆盖一个刷新按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38859735/

相关文章:

ios - 将 UITextView 占位符/文本放置在左上角

ios - 表刷新使数组项的数量加倍

ios - 仅在下方的一个 View 上绘制阴影

ios - Apple 登录域验证失败

ios - Swift 的事件指示器仅覆盖部分屏幕

swift - iOS 13 模拟器 : reverseGeocodeLocation - [GEOAddressObject] [NSLocale currentLocale] failed for NSLocaleCountryCode@

ios - 是否可以在 iOS 框架内获取位置?

javascript - IOS WKWebView 和 UIWebView 没有加载 JavaScript 注册表单,而 Safari 可以

ios - 如何在Cocoa Touch Library中添加UIWebView并在应用程序中使用它

javascript - 尝试使用 highcharts 将数据点注入(inject) iOS webView