ios – UIViewRepresentable doesn’t seem to be picking up changes of Bindable value


I am working on a custom input who’s purpose is to present emoji keyboard to the user, doing so required to use UIKit UITextField so I had to wrap it in UIViewRepresentable as shown below. But for some reason I am not seeing any changes of text when picking a different emoji.

Custom Component

import SwiftUI


class EmojiInputUiKit: UITextField {
  override var textInputMode: UITextInputMode? {
    .activeInputModes.first(where: { $0.primaryLanguage == "emoji" })
  }
}


struct EmojiInput: UIViewRepresentable {
  @Binding var text: String
  
  func makeUIView(context: Context) -> EmojiInputUiKit {
    EmojiInputUiKit()
  }
  
  func updateUIView(_ uiView: EmojiInputUiKit, context: Context) {
    uiView.text = text
  }
}

Usage

import SwiftUI

struct EmojiPicker: View {
  @State private var emoji = "👋"
  @FocusState private var focused
  
  var body: some View {
    ZStack {
      Text(emoji) // Text value here doesn't change from 👋
      EmojiInput(text: $emoji)
        .opacity(0)
        .focused($focused)
        .allowsHitTesting(false)
    }
    .onChange(of: emoji) {
      focused = false
    }
    .onTapGesture {
      focused = true
    }
  }
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img