2

I have encountered a bug whenever UIViewControllerRepresentable is used within a SwiftUI view. I've filed a report to Feedback Assistant, but need to work around it to release my app.

import SwiftUI

struct ContentView: View {
    var body: some View {
        GeometryReader { geo in
            VStack {
                Text("geo.size.height: \(geo.size.height), geo.safeAreaInsets.top: \(geo.safeAreaInsets.top)")
                    .padding()
                TestGeo()
            }
        }
    }
}

struct TestGeo: UIViewControllerRepresentable, Equatable {
    func makeUIViewController(context: UIViewControllerRepresentableContext<Self>) -> UIViewController { return UIViewController() }
    func updateUIViewController(_ viewController: UIViewController, context: UIViewControllerRepresentableContext<Self>) {}
}

Note, the GeometryReader is not required for the bug to manifest but it might shed light on what is happening.

Steps to reproduce:

Run code on a notchless iPhone like the iPhone 8 Plus simulator in portrait. Rotate simulator from portrait to landscape. Rotate simulator from landscape to portrait.

The text reads:

geo.size.height: 736.0, geo.safeAreaInsets.top: 0.0

but should read:

geo.size.height: 716.0, geo.safeAreaInsets.top: 20.0

Also, the text position is moved 20 points too high.

An interesting way I've found of manually correcting the layout is to start to pull down the Notification Center, the layout and safe area heights will correct themselves.

Any ideas on how to make the layout correct itself?

Steve M
  • 9,296
  • 11
  • 49
  • 98

1 Answers1

0

Wrapping in a navigation controller seems to do the trick, just disable all unwanted UI effects such as the navigation bar:

func makeUIViewController(context: UIViewControllerRepresentableContext<Self>) -> UIViewController {
    let navigationController = UINavigationController(rootViewController: UIViewController())
    navigationController.isNavigationBarHidden = true
    return navigationController
}
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220