SwiftUI: crash when tapping the TabView's tab

Viewed 379

I got a screen with TabView which holds two tabs:

...

var body: some View {
    TabView() {
        ScreenViewOne().tabItem {
            Image(systemName: "calendar")
            Text("Screen one")
        }.tag(0)
        ScreenViewTwo().tabItem {
            Image(systemName: "list.bullet")
            Text("Screen two")
        }.tag(1)
    }
}

...

When I tap Screen two tab, the app crashes with the following error:

precondition failure: unknown attribute: 4294967295

If I use the same screen for both tabs like shown below, everything works as expected and there is no crash:

...

var body: some View {
    TabView() {
        ScreenViewOne().tabItem {
            Image(systemName: "calendar")
            Text("Screen one")
        }.tag(0)
        ScreenViewOne().tabItem {
            Image(systemName: "list.bullet")
            Text("Screen two")
        }.tag(1)
    }
}

...

Changing the order of the screens, their content, etc. doesn't help.

1 Answers

It turned out that this was caused by the List element in ScreenViewOne which has multiple sections in it:

var body: some View {
List {
  Section(header: Text("Section 1")) {
    ForEach(items) { item in
      Text("\(item)")
    }
  }
  .backgroundColor(Color.white)
  Section(header: Text("Section 2")) {
    ForEach(items) { item in
      Text("\(item)")
    }
  }
  .backgroundColor(Color.white)
}

}

More specifically, by the presence of backgroundColor modifier which caused the issue.

To resolve it, I removed both modifiers and implemented UITableViewHeaderFooterView.appearance().tintColor = UIColor.clear in the custom init method instead:

init(factory: Factory) {
    ...        

    UITableViewHeaderFooterView.appearance().tintColor = UIColor.clear 

    ...
  }
Related