首页 > 解决方案 > SwiftUi 按钮操作重新加载 WebView

问题描述

我有简单的 SwiftUi WebView 代码,我想在单击按钮时重新加载 WebView(我只添加了函数和简单代码)。

class myWebViewController: UIViewController, WKNavigationDelegate {


    var webview: WKWebView

    func reload(){
        webview.reload()
    }

}


   override func viewDidLoad() {
        super.viewDidLoad()


       let overlayView = UIHostingController(rootView: NewRecordButton())
       addChild(overlayView)

       overlayView.view.backgroundColor = UIColor.clear
        overlayView.view.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
       overlayView.view.isUserInteractionEnabled = true


       self.view.addSubview(overlayView.view)
       overlayView.didMove(toParent: self)

    }

    struct NewRecordButton: View {
        @State var color = false
        var body: some View {

                    HStack {
                        Spacer()
                       Button(action: {

                        myWebViewController().webview.reload() // HERE MY ACTION BUT DONT WORK

                                           }, label: {
                                               Text("+")
                                               .font(.system(.largeTitle))
                                               .frame(width: 35, height: 35)
                                               .foregroundColor(Color.white)
                                               .padding(.bottom, 7)


                                           })
                                           .background(Color.blue)
                                           .cornerRadius(38.5)
                                           .padding()
                                           .shadow(color: Color.black.opacity(0.3),
                                                   radius: 3,
                                                   x: 3,
                                                   y: 3)
             }

        }
    }
}

标签: swiftbuttonswiftuiwkwebview

解决方案


myWebViewController() 创建 Web 视图的新实例


推荐阅读