Skip to content

Instantly share code, notes, and snippets.

@gfontenot
Last active April 17, 2020 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfontenot/935e7f6ee51d6c74d31e2339459a4abf to your computer and use it in GitHub Desktop.
Save gfontenot/935e7f6ee51d6c74d31e2339459a4abf to your computer and use it in GitHub Desktop.
Demonstration of weird WKWebView behavior when paired with SwiftUI
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
}
}
}
@gfontenot
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment