Skip to content

Instantly share code, notes, and snippets.

@krzyzanowskim
Created January 5, 2022 23:23
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 krzyzanowskim/8feb56819b944385cb9aef6ff6d778c9 to your computer and use it in GitHub Desktop.
Save krzyzanowskim/8feb56819b944385cb9aef6ff6d778c9 to your computer and use it in GitHub Desktop.
import SwiftUI
import PlaygroundSupport
struct TestView: View {
@State var text: String = ""
@State var dummy: String = "dummy"
@State var showTextField1: Bool = true
@State var showTextField2: Bool = true
var body: some View {
VStack {
if showTextField1 {
TextField(
"",
text: $text,
onEditingChanged: { editing in
print("editing \(editing)") // <- not called on textfield hide
},
onCommit: {
print("commit") // <- not called on textfield hide
}
)
}
Button("toggle") {
showTextField1.toggle()
}
if showTextField2 {
MyTextFieldView()
}
Button("toggle") {
showTextField2.toggle()
}
}
.frame(width: 100)
}
}
struct MyTextFieldView: NSViewRepresentable {
func makeNSView(context: Context) -> NSTextField {
let field = MyTextField()
return field
}
func updateNSView(_ nsView: NSTextField, context: Context) {
}
}
class MyTextField: NSTextField {
override func textDidEndEditing(_ notification: Notification) {
print("textDidEndEditing") // <- called on hide textfield
super.textDidEndEditing(notification)
}
}
PlaygroundPage.current.setLiveView(TestView())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment