Created
October 19, 2021 19:25
-
-
Save mattyoung/1fc22fdb09abb2423bec6b971c92131b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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