我对 Swift 和 SwiftUI 很陌生。我想通过 WebKit:WKWebView 显示 webview。
我尝试如下实现它:
var webView: WKWebView? {
didSet {
guard let webView = webView else { return }
webView.frame = view.frame
view.addSubview(webView)
}
}
您可以从这里找到更多详细信息:https://github.com/sbertix/Swiftagram/issues/48当我实现它时,它如下所示:
但我想自定义 WebView 的内容,如下所示:
有没有办法像上面那样实现它?
最佳答案
最简单的就是将这三个 View (header-webview-footer)包裹在 VStack
中。 ,即如下所示(当然你的页眉页脚可以是任何)
注:WebView
任何您的 WKWebView 包装器(我从 https://stackoverflow.com/a/61059318/12299030 进行测试)
struct DemoView: View {
var body: some View {
VStack(spacing: 0) {
HeaderView()
WebView(url: URL(string: "https://www.stackoverflow.com"))
FooterView()
}
}
struct HeaderView: View {
var body: some View {
Text("Header Note")
.foregroundColor(.yellow)
.frame(maxWidth: .infinity, minHeight: 60)
.background(Color.red)
}
}
struct FooterView: View {
var body: some View {
Text("Footer Note")
.foregroundColor(.yellow)
.frame(maxWidth: .infinity, minHeight: 100)
.background(Color.red)
}
}
关于ios - 在 SwiftUI WebView 中自定义内容(添加页脚和页眉注释),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63335974/