I have this weird SwiftUI keyboard animation bug that is currently live in one of my apps and I am struggling to fix it and would appreciate any help in understanding the underlying problem and how to solve it.
Below is example code made up of simple view that contains a TextField and a Footer Button. I set the TextField to get focus after 0.75 seconds when the view appears.
Unfortunately, the below code results in an animation bug where the background behind the footer button flashes briefly as the keyboard finishes animating on to the screen.
struct NameTextFieldView: View {
@Environment(.dismiss) var dismiss
@FocusState private var isFocused: Bool
@State private var name: String = ""
var body: some View {
NavigationStack {
Form {
TextField("Name", text: $name)
.focused($isFocused)
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.75) {
isFocused = true
}
}
}
.safeAreaInset(edge: .bottom) {
Button(action: {
dismiss()
}, label: {
Text("Add")
.foregroundStyle(Color.white)
})
.frame(width: 200, height: 44)
.background(Color.blue)
.clipShape(RoundedRectangle(cornerSize: CGSize(width: 20, height: 10)))
.padding()
}
.navigationTitle("Add Name")
.navigationBarTitleDisplayMode(.inline)
}
}
}
Odd animation bug.