Home > Enterprise >  How can we show suggestions for UITextField is xCode Swift?
How can we show suggestions for UITextField is xCode Swift?

Time:06-19

I have spent several hours on internet, but I am unable to find suitable solution for my problem. The problem is following: I have implemented text filed, (with the help of stackoverflow post) using UIViewRepresentable, which wraps UITextField. I did this in order to be able to gracefully make text input first responder before IOs15. I am using this text field in many places and many different pages.

Currently we have need to show suggestions in form of dropdown, when user starts typing text in our custom text field. The view that will be shown, should appear exactly below of the each text field and it should not disrupt the flow and positioning of existing views. Of course, we need to modify our text field implementation, because we are using it in so many different places different approach looks very hard.

In general how is it possible to implement this kind of behavior?

Bellow I am posting our implementation of text input

Thanks


class TextFieldPad: UITextField {
  let padding = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)
  override open func textRect(forBounds bounds: CGRect) -> CGRect {
    return bounds.inset(by: padding)
  }
  override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
    return bounds.inset(by: padding)
  }
  override open func editingRect(forBounds bounds: CGRect) -> CGRect {
    return bounds.inset(by: padding)
  }
}
struct FocusableTextField: UIViewRepresentable {
  class Coordinator: NSObject, UITextFieldDelegate {
    @Binding var text: String
    var didBecomeFirstResponder = false
    init(text: Binding<String>) {
      _text = text
    }
    func textFieldDidChangeSelection(_ textField: UITextField) {
      text = textField.text ?? “”
    }
  }
  @Binding var placeHolder: String;
  @Binding var text: String
  var isFirstResponder: Bool = false
  var issecure: Bool = false
  var borderColor: UIColor = UIColor(rgb: 0xEEA924);
  var backgroundColor: UIColor = UIColor.white;
  var placeholderColor: UIColor = UIColor.black;
  var textColor: UIColor = UIColor.black;
  func makeUIView(context: UIViewRepresentableContext<FocusableTextField>) -> UITextField {
    let textField = TextFieldPad(frame: .zero)
    textField.layer.borderWidth = 2;
    textField.layer.borderColor = borderColor.cgColor
    textField.layer.cornerRadius = CGFloat(14.0)
    textField.layer.backgroundColor = backgroundColor.cgColor
    textField.isSecureTextEntry = issecure
    textField.textColor = textColor
    textField.autocorrectionType = .no
    textField.autocapitalizationType = .none
    textField.attributedPlaceholder = NSAttributedString(
      string: placeHolder,
      attributes: [NSAttributedString.Key.foregroundColor : placeholderColor.cgColor]
    )
    textField.delegate = context.coordinator
    return textField
  }
  func makeCoordinator() -> FocusableTextField.Coordinator {
    return Coordinator(text: $text)
  }
  func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<FocusableTextField>) {
    uiView.text = text
    if isFirstResponder && !context.coordinator.didBecomeFirstResponder {
      uiView.becomeFirstResponder()
      context.coordinator.didBecomeFirstResponder = true
    }
  }
}

CodePudding user response:

I ended up using inputAccessoryView and displaying scrollable suggestions inside that view over keyboard.

Any alternative opinions and suggestions are welcome :)

  • Related