SwiftUI animation glitch with search bar and Scroll view (iOS 16.4 / SwiftUI 4)

Animation is glitching when I tap on search bar

Below is the code

struct CustomTabBar: View {
    @State var searchText = ""
    var data = [1,2,3,4,5,6,7,8,9,10]
    var body: some View {
        NavigationStack {
            ScrollView(.vertical) {
                ForEach(data, id: \.self) { d in
                    box
                }
            }
            .navigationTitle("Search")
            .searchable(text: $searchText)
        }
    }
    
    var box: some View {
        Rectangle()
            .frame(width: 75, height: 50)
            .cornerRadius(10)
    }
}

I tested in simulator and did not notice a problem. What is precisely the glitch you have ?

When the search bar is tapped, the scroll view jumps to the top instead of animating smoothly. You can see the same in the gif I attached. Occasionally, it works smoothly for me as well but majority of the time it fails.

Where do you test ? Device ? Simulator ? Preview ?

For me that's a smooth move to the top (in simulator)

I captured as creen shot video. On the mp4 it is OK (but cannot attach here on the forum), but when saved in gif, it is not as good…

Here is a test capture (I set random colors for the nicety).

You can clearly see how the bottom view with rectangles just jump up with no smooth animation in your example as well. Focus on the blue rectangle if you want to see it clearly.

Still have this issue with iOS 17, did you find a solution ?

The solution is very simple: stop using SwiftUI, which is a broken mess, and instead use UIKit, where all this Just Works®.

SwiftUI animation glitch with search bar and Scroll view (iOS 16.4 / SwiftUI 4)
 
 
Q