Skip to content

Instantly share code, notes, and snippets.

@mattyoung
Created October 19, 2021 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattyoung/1fc22fdb09abb2423bec6b971c92131b to your computer and use it in GitHub Desktop.
Save mattyoung/1fc22fdb09abb2423bec6b971c92131b to your computer and use it in GitHub Desktop.
import SwiftUI
struct ContentView: View {
@FocusState var isFocused: Bool
@State var flag = true
@State var a = ""
@State var b = ""
var body: some View {
VStack {
TextField("Enter a", text: $a)
.focused($isFocused)
TextField("Enter b", text: $b, prompt: Text("Enter b"))
Text("isFocused: \(isFocused ? "Yes" : "No")")
Toggle("Show Xxxx ", isOn: $flag)
}
.onAppear {
// must delay changing isFocused, if not then no focus
// is this a bug?
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
isFocused = true
}
}
.toolbar {
ToolbarItem(placement: .keyboard) {
// @ViewBuilder ifView don't work: seems when isFocused change do not trigger update
if isFocused {
Button("Focused") {}
}
// Behavior of this ifView is odd: if initial value is true, can turn off
// but oddly, when turn off the above view then show even when isFocused is false!
// if initial value is false, cannot turn on!
if flag {
Button("Xxxx") {}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment