I understand needing Instruments to diagnose issues with my own animations etc, but needing to debug the built-in animations for such a basic example would suggest to me that I'm doing something fundamentally wrong which I imagine would be obvious to someone with more experience than me, or SwiftUI is just very buggy.
If we take one of the examples written by @twoStraws here:
https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-a-search-bar-to-filter-your-data
I have modifiered it slightly to show the border of each View for debug purposes:
import SwiftUI
extension View {
func debugModifier<T: View>(_ modifier: (Self) -> T) -> some View {
#if DEBUG
return modifier(self)
#else
return self
#endif
}
}
extension View {
func debugBorder(_ color: Color = .red, width: CGFloat = 1) -> some View {
debugModifier {
$0.border(color, width: width)
}
}
}
struct ContentView: View {
let names = ["Holly", "Josh", "Rhonda", "Ted"]
@State private var searchText = ""
var body: some View {
NavigationStack {
List {
ForEach(searchResults, id: \.self) { name in
NavigationLink {
Text(name)
} label: {
Text(name)
.debugBorder()
}
}
}
.navigationTitle("Contacts")
.debugBorder()
}
.searchable(text: $searchText)
.debugBorder()
}
var searchResults: [String] {
if searchText.isEmpty {
return names
} else {
return names.filter { $0.contains(searchText) }
}
}
}
You can see the problem again from the iOS Simulator in the following GIF.
The animation issue happens the first time you focus the Search field, but not the second time.
On my physical iOS device the problem occurs every time though, which does suggest the performance of the device may be relevant.
It's hard to believe that the basic built-in components would be this buggy, so it leads me to believe that there could be some settings I'm missing to improve the app's performance. I have tried switching to the Release profile in Xcode but this doesn't improve things.
Alternatively this is just the state of SwiftUI and I need to accept that it's far from perfect, or use UIKit?