I'm trying to focus the isUsernameFocused
textField as soon as it loads on the screen, I tried doing it directly in the onAppear
method but it looks like it needs a delay in order for it to focus. My concern is that for some reason the focus only occurs with a delay greater than 0.6 fractions of a second. Setting it at 0.7
fractions of a second seems to work fine but I'm afraid that eventually, this will stop working if the view gets bigger since it will need more time to load.
Is there a way to know when the VStack
is fully loaded so I can trigger the isUsernameFocused
? Something like, viewDidLoad
in UIKit.
struct ContentView: View {
@FocusState private var isUsernameFocused: Bool
@State private var username = ""
var body: some View {
VStack {
TextField("Username", text: $username)
.focused($isUsernameFocused)
}
.onAppear{
DispatchQueue.main.asyncAfter(deadline: .now() + 0.7){
self.isUsernameFocused = true
}
}
}
}