I am working on a tvOS app with SwiftUI. I have black as theme colour. When my textfield is in focus I want text colour to be black and when field is not in focus I want my text colour to be white. I am trying to use focusable method but it is not working. When running on simulator and the respective fields are in focus, it does not log anything in the debug console.
@State private var isEmailFieldInFocus: Bool = false
@State private var isPasswordFieldInFocus: Bool = false
ZStack {
Color.black.edgesIgnoringSafeArea(.all)
VStack(spacing: 50) {
TextField("", text: self.$emailAddress)
.foregroundColor(self.isEmailFieldInFocus ? Color.black : Color.white)
.border(self.isEmailFieldInFocus ? Color.clear : Color.white, width: 1)
.focusable(true) { (focus) in
self.isEmailFieldInFocus = focus
if focus {
print("Email field is in focus")
}
}
TextField("", text: self.$password)
.foregroundColor(self.isPasswordFieldInFocus ? Color.black : Color.white)
.border(self.isPasswordFieldInFocus ? Color.clear : Color.white, width: 1)
.focusable(true) { (focus) in
self.isPasswordFieldInFocus = focus
if focus {
print("Password field is in focus")
}
}
}
}