Folks, I'm trying to understand what I'm missing here. In a SwiftUI project, I've wrapped a UITextView in UIViewRepresentable as a replacement for SwiftUI's TextEditor control, largely so that I can get it to become/resign first responder status.
This minimally-viable example works fine, except for one bug that I can't seem to resolve: if I add an emoji anywhere in that text view, then text insertion goes haywire — wherever I have the cursor, the character typed will be inserted, but then the cursor immediately jumps to the end of the text in the field.
import SwiftUI
struct WrappedTextView: UIViewRepresentable {
		class Coordinator: NSObject, UITextViewDelegate {
				@Binding var text: String
				var didBecomeFirstResponder: Bool = false
				init(text: Binding<String>) {
						_text = text
				}
				func textViewDidChangeSelection(_ textView: UITextView) {
						DispatchQueue.main.async {
								self.text = textView.text ?? ""
						}
				}
		}
		@Binding var text: String
		var isFirstResponder: Bool = false
		func makeUIView(context: UIViewRepresentableContext<WrappedTextView>) -> UITextView {
				let textView = UITextView()
				textView.delegate = context.coordinator
				return textView
		}
		func makeCoordinator() -> WrappedTextView.Coordinator {
				return Coordinator(text: $text)
		}
		func updateUIView(_ uiView: UIViewType, context: UIViewRepresentableContext<WrappedTextView>) {
				uiView.text = text
				if isFirstResponder && !context.coordinator.didBecomeFirstResponder {
						uiView.becomeFirstResponder()
						context.coordinator.didBecomeFirstResponder = true
				}
		}
}
Credit to this Stack Overflow answer for the above code: https://stackoverflow.com/a/56508132
Can someone help me understand what I'm doing wrong?
There's an example project available here, wrapping both UITextField and UITextView in UIViewRepresentable: https://github.com/AngeloStavrow/WrappedTextInputExample
Post
Replies
Boosts
Views
Activity
Hi all,
I'm trying to detect an app launch in a multiplatform SwiftUI app (iOS 14/macOS 11) and having a heck of a time getting onReceive() handlers to catch didFinishLaunchingNotification, despite other notifications are being received as expected.
Here's a simplified version of what I'm trying:
import SwiftUI
struct ContentView: View {
		#if os(iOS)
		let didFinishLaunchingNotification = UIApplication.didFinishLaunchingNotification
		let didBecomeActiveNotification = UIApplication.didBecomeActiveNotification
		#else
		let didFinishLaunchingNotification = NSApplication.didFinishLaunchingNotification
		let didBecomeActiveNotification = NSApplication.didBecomeActiveNotification
		#endif
		var body: some View {
				NavigationView {
						SidebarView()
						ListView()
						Text("Content!")
				}
				.onReceive(NotificationCenter.default.publisher(for: didFinishLaunchingNotification)) { _ in
						print("didFinishLaunchingNotification fired")
				}
				.onReceive(NotificationCenter.default.publisher(for: didBecomeActiveNotification)) { _ in
						print("didBecomeActiveNotification fired")
				}
		}
}
Do you all see anything wrong with this approach? Is this a bug in SwiftUI?
Thanks in advance!