InputBarAccessoryView
InputBarAccessoryView copied to clipboard
Swift UI Support
Currently trying to implement a UIViewRepresentable of InputBarAccessoryView
with no luck. Is there any branch or project that I could check out that had implemented this feature successfully?
Could you please check SwiftUI example in MessageKit example app? https://github.com/MessageKit/MessageKit
@fpedro23 did you get any further with your attempt?
Yep, heres the code:
struct CommentInputBar: UIViewRepresentable {
@Binding
var text: String
@Binding
var size: CGSize
var onSendPerform: (String) -> Void
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> InputBarAccessoryView {
let bar = iMessageInputBar()
bar.backgroundView.backgroundColor = .acBackground
bar.delegate = context.coordinator
DispatchQueue.main.async {
bar.inputTextView.placeholderLabel.text = "Write a review..."
}
return bar
}
func updateUIView(_ uiView: InputBarAccessoryView, context: Context) {
}
class Coordinator: InputBarAccessoryViewDelegate {
var control: CommentInputBar
init(_ control: CommentInputBar) {
self.control = control
}
func inputBar(_ inputBar: InputBarAccessoryView, didChangeIntrinsicContentTo size: CGSize) {
control.size = size
}
func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
control.onSendPerform(text)
inputBar.inputTextView.text = ""
}
}
}
struct YourCustomView: View {
@State
var size: CGSize = CGSize(width: 0, height: 50)
var body: some View {
CommentInputBar(text: $text, size: $size, onSendPerform: {
//Do your action
})
.frame(height: size.height)
}
}
It is important to follow this @Binding size approach since SwiftUI and the internal constraints of InputBarAccessoryView
seem to conflict and the height of the Accessory view is not updated as the user types, thats why I manually set the height of the frame. Let me know if you have any other questions.
This is awesome, thanks for sharing!
I haven't seen this strategy of binding a size to a SwiftUI control, and listening to UIKit events, is it something you found elsewhere?
I came up with this solution after trying for 6 hours straight to figure out why the bar had such odd sizing. I don't think it's a standard approach, I just did it this way for this particular view since there are some internal constraints that I do not control and InputBarAccessoryViewDelegate
makes it really easy to listen for size changes.