swift - 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)
}
}
}
}
解决方案
myWebViewController() 创建 Web 视图的新实例
推荐阅读
- javascript - 最近的 Blogspot 更新中断了 web 扩展的 window.getSelection()。有什么建议的解决方法吗?
- authentication - 托管在专用网络中并使用 Azure AD 作为 oauth 提供程序时,Keycloak 引发 UnknownHostException
- python - 熊猫的相关代码在哪里?
- firebase - 如何为来自另一个firebase项目的用户设置/传播firebase身份验证上下文(UID)以进行firestore规则配置?
- amazon-web-services - 是否可以在特定时间范围内对 S3 事件进行分组?
- python - 熊猫仅在数据框中获取包含另一列中某个字符串的行的最小/最大值
- spring - spring boot如何使用jpa查询将项目附加到列表中
- android - Android 10+ 电话录音支持
- powershell - New-DFSNFolderTarget cmdlet 的问题
- javascript - 当我使用图表 div 的样式属性设置高度时,水平条形图显示在画布下方(Chart.js 2.9.4)