简体   繁体   English

SwiftUI:点击 TabView 的选项卡时崩溃

[英]SwiftUI: crash when tapping the TabView's tab

I got a screen with TabView which holds two tabs:我有一个带有 TabView 的屏幕,其中包含两个选项卡:

...

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前提条件失败:未知属性: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.更改屏幕的顺序、它们的内容等无济于事。

It turned out that this was caused by the List element in ScreenViewOne which has multiple sections in it:原来这是由 ScreenViewOne 中的 List 元素引起的,其中包含多个部分:

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.更具体地说,由于存在导致问题的backgroundColor修饰符。

To resolve it, I removed both modifiers and implemented UITableViewHeaderFooterView.appearance().tintColor = UIColor.clear in the custom init method instead:为了解决它,我删除了两个修饰符并在自定义 init 方法中实现了UITableViewHeaderFooterView.appearance().tintColor = UIColor.clear

init(factory: Factory) {
    ...        

    UITableViewHeaderFooterView.appearance().tintColor = UIColor.clear 

    ...
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM