Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Animation bug?

I'm having this odd issue with macOS Montery 3 & Xcode 13 Beta 3, where I get this error with List's and animations:

[General] Row index 0 out of row range (numberOfRows: 0) for <SwiftUIListCoreOutlineView: 0x133885000>

Its kinda hard to explain, but heres a simple reproduction:

Minimal Reproducible Example

  1. Create a new SwiftUI macOS application
  2. Paste this code:
struct ContentView: View {
    @State var items = ["Item"]
    
    @ViewBuilder var mainView: some View {
        if items.isEmpty {
            Text("Im empty")
        }
        else {
            List(items, id: \.self) {s in
                Text(s)
            }
        }
    }
    var body: some View {
        NavigationView {
            mainView
                .toolbar {
                    Button(action: {
                        withAnimation {
                            items.removeAll()
                        }
                    }) {
                        Image(systemName: "minus")
                    }
                }
            Text("Second")
        }
    }
}
  1. Run the app, and try resizing the sidebar. (You should be able to)
  2. Then press the minus button on the toolbar. This simply removes all the items.
  3. Then, the resizing of the sidebar should be broken. You might also get a bunch of errors in the console.

Is anybody able to reproduce this issue, and is it a bug in SwiftUI?

like image 968
newswiftuser Avatar asked Mar 21 '26 04:03

newswiftuser


1 Answers

got the same behavior. The code works well if I remove the "withAnimation". Or put "items.removeAll()" outside the "withAnimation"

like image 149
workingdog support Ukraine Avatar answered Mar 23 '26 21:03

workingdog support Ukraine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!