Here's the simplified version of my issue, I originally ran into it with my existing project while trying to move to swiftui.
I start a new project with "Tabbed App", select "swiftui" as the UI element.
Then I create 4 storyboard files named "Test1.storyboard" .... "Test4.storyboard" each with a single UIView controller and a uilabel with "Test 1".... "Test 4" written respectively, I check the "is initial view controller" box on all of them.
To load the views I create a subclass of UIViewControllerRepresentable:
struct PlainStoryboardViewController: UIViewControllerRepresentable {
var storyBoardName: String
func makeUIViewController(context: Context) -> UIViewController {
debugPrint("Showing \(self.storyBoardName)")
let storyBoard = UIStoryboard(name: self.storyBoardName, bundle: nil)
let vc = storyBoard.instantiateInitialViewController()!
return vc
}
func updateUIViewController(_ pageViewController: UIViewController, context: Context) {
}
}
Now I just replace the Text("blah") in the generated ContentView with my PlainStoryboardViewController, and add the other 2 extra buttons, and that's all I do... but for completeness here it is:
struct ContentView: View {
@State private var selection = 0
var body: some View {
TabView(selection: $selection){
PlainStoryboardViewController(storyBoardName: "Test1")
.font(.title)
.tabItem {
VStack {
Image("first")
Text("First")
}
}
.tag(0)
PlainStoryboardViewController(storyBoardName: "Test2")
.font(.title)
.tabItem {
VStack {
Image("second")
Text("Second")
}
}
.tag(1)
PlainStoryboardViewController(storyBoardName: "Test3")
.font(.title)
.tabItem {
VStack {
Image(systemName: "pencil")
Text("Third")
}
}
.tag(2)
PlainStoryboardViewController(storyBoardName: "Test4")
.font(.title)
.tabItem {
VStack {
Image(systemName: "trash")
Text("Fourth")
}
}
.tag(3)
}
}
}
It seems straight forward, and I expect to load the respective pages when I click on the 4 buttons, which it does.... on the simulator, my iphone, but then it fails on the ipad.
It would sometimes load the correct page, but most of the time it just refreshes the current page instead (I printed under ViewDidAppear on my original project, but I assume it does the same here). So for example I'm on "Second" and I click on "Third", and I expect it to load "Test 3", but it would either go to "Test 3" or refresh "Test 2". You wait awhile and try again and it would either work or just do another refresh.
This is tested and the issue is reproduced with an iPad mini4 and ipad pro 11.5-inch running under ios 13.7. But it worked as expected with an iphone X also running under ios 13.7. The project is built with xcode 11.7 deployment target 13.7