简体   繁体   English

如何使用 SwiftUI 去除 WKWebView 上方的空白

[英]How to get rid of white space above WKWebView with SwiftUI

I am building WKWebViews into my SwiftUI app.我正在将 WKWebViews 构建到我的 SwiftUI 应用程序中。 I am using the following implementation:我正在使用以下实现:

struct UrlWebView: UIViewRepresentable {
    typealias UIViewType = WKWebView
    
    
    
    var urlToDisplay: URL
    
    
    func makeUIView(context: Context) -> WKWebView {
        let webView = WKWebView()
        
        webView.load(URLRequest(url: urlToDisplay))
        
        return webView
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
        
    }
}


struct WebView: View {
    var game: Game
    
    var body: some View {
        UrlWebView(urlToDisplay: game.url)
    }
}

initiated by发起者

 NavigationLink(destination: WebView(game: game))

There is some strange behavior that happens when I scroll where there is white space under the navigation header and above the web page...当我滚动导航标题下方和网页上方的空白区域时,会发生一些奇怪的行为......

iOS sim in WKWebView with white space WKWebView 中带有空白的 iOS sim

How can I get rid of the white space on top of the WKWebView (WHEN I SCROLL DOWN) while still keeping it scrollable?我怎样才能摆脱 WKWebView 顶部的空白(当我向下滚动时),同时仍然保持它可滚动?

Thank you so much.太感谢了。

EDIT: When I swipe down, there is no bounce back.编辑:当我向下滑动时,没有反弹。 Do you know how to fix this?你知道如何解决这个问题吗?

I tested out your code, but I am not sure which white space you are referring to because I did not find anything beside a full WebView() on screen and the system back button.我测试了你的代码,但我不确定你指的是哪个空白,因为除了屏幕上的完整 WebView() 和系统后退按钮之外,我没有找到任何东西。

However, if you are using Xcode version 13 and below, you can try this, and let me know if the white space disappear after.但是,如果您使用的是 Xcode 13 及以下版本,您可以试试这个,如果空白消失后请告诉我。

struct WebView: View {
var body: some View {
    NavigationView {
        NavigationLink {
            GameTabView(urlToDisplay: URL(string: "https://stackoverflow.com/questions/72665439/how-to-get-rid-of-white-space-above-wkwebview-with-swiftui")!)
                .navigationBarHidden(true)
        } label: {
            Text("Open")
        }
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM