Created
January 5, 2022 23:23
-
-
Save krzyzanowskim/8feb56819b944385cb9aef6ff6d778c9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
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