Last active
April 17, 2020 01:12
-
-
Save gfontenot/935e7f6ee51d6c74d31e2339459a4abf to your computer and use it in GitHub Desktop.
Demonstration of weird WKWebView behavior when paired with SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
@State var showModal: Bool = false | |
var body: some View { | |
NavigationView { | |
VStack { | |
WebView(url: URL(string: "https://www.example.com")!) | |
Button("Fixme") { self.showModal = true } | |
} | |
}.sheet(isPresented: $showModal) { Text("Dismiss Me") } | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
import WebKit | |
struct WebView: UIViewRepresentable { | |
let url: URL | |
final class Coordinator { | |
var alreadyLoaded: Bool = false | |
} | |
init(url: URL) { | |
self.url = url | |
} | |
func makeCoordinator() -> Coordinator { | |
return Coordinator() | |
} | |
func makeUIView(context: Context) -> WKWebView { | |
return WKWebView() | |
} | |
func updateUIView(_ uiView: WKWebView, context: Context) { | |
if !context.coordinator.alreadyLoaded { | |
uiView.load(URLRequest(url: url)) | |
context.coordinator.alreadyLoaded = true | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adding this to a new project and running it demonstrates the issue I'm hitting. When the app initially loads, the web view is inset on the top as if it's doubling up on the margin for the navigation bar. When the "Fixme" button is tapped, it corrects itself.