SwiftUI view gets wider than host/parent view?

I created a UIHostingController to put a SwiftUI view in my app. The root view contains a VStack, and inside that some HStacks and Pickers. The picker pushes the view wider than the screen. I can see in Xcode (Debug View Hierarchy) that my screen is 320 (iPhone SE) and my SwiftUI View is 328. Usually I would fix this with auto layout constraints pinning left and right edges to parent with priority 1000. What is the equivalent in SwiftUI?


Rob

Replies

I used one of my tech support tickets and was told it's a bug in SwiftUI. If I can't even do a simple layout like this, I'll go back to UIKit for now. If anyone knows how to fix it, I'd love to know.


Text in top HStack is completely offscreen. Pickers are partly off screen. (Running on iPhone SE size screen is worst; other screen sizes also have problems.)


struct ContentView: View {
    
    @State var choice: Int = 10
    @State var choice2: Int = 10
    
    var body: some View {
        return VStack {
            HStack {
                Text("Some text here")
                Spacer()
                Text("Foo baz")
            }
            HStack {
                Picker(selection: self.$choice, label: Text("C1")) {
                    ForEach(0..<10) { n in
                        Text("\(n) a").tag(n)
                    }
                }
                Picker(selection: self.$choice2, label: Text("C2")) {
                    ForEach(0..<10) { n in
                        Text("\(n) b").tag(n)
                    }
                }
            }
        }
    }
}

Have you heard anything about this bug?

There have been no meaningful updates to my bug report, but I posted about this problem again in another thread and someone gave a good workaround, at least for my specific case:


https://forums.developer.apple.com/thread/127028