0

I'm trying to keep my code for removing list separators in iOS 14 while adopting the new .listRowSeparatorVisibility for iOS 15 onward.

extension View {
  func listRowModifier(color: Color) -> some View {
    let view = self
      .frame(maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .leading)
      .background(color)
    
    if #available(iOS 15, *) {
      return AnyView(
        view
          .listRowSeparatorVisibility(.hidden)
          .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
      )
    } else {
      return AnyView(
        view
          .listRowInsets(EdgeInsets(top: -1, leading: 0, bottom: -1, trailing: 0))
      )
    }
  }
}

This crashes with an EXC_BAD_ACCESS

#0  0x0000000186e84214 in swift_getOpaqueTypeMetadata ()
#1  0x0000000100dc2b50 in View.listRowModifier(color:) ()

Crashes on this line in the View:

List {
  Spacer().frame(height: headerHeight + Theme.unit(2)).listRowModifier()
  ...
}

Other things I've tried:

  1. Using @ViewBuilder instead, also crashes
  2. Doing the if else directly in the View instead of using an extension, also crashes

** Update **

When the views returned within the if and else block are identical, it works. If not, it crashes. I'm not sure how that's useful consider the whole point of this is that the views are different because one has a feature that the other doesn't...

chrysb
  • 1,351
  • 2
  • 13
  • 20

1 Answers1

0

Solved

The issue was that .listRowSeparatorVisibility was deprecated in iOS 15. I changed it to .listRowSeparator and it stopped crashing.

That was not clear at all, but I'll leave this here in case anyone else runs into this issue.

chrysb
  • 1,351
  • 2
  • 13
  • 20