Search code examples
iosswiftuitextfield

How to toggle show and hide LeftView/RightView of UITextField?


Currently, I set my textfield with LeftView ( icon ) but I want to toggle show it when click some button.

example image

My code.

let imageIconLeftView = UIImageView(image: UIImage())
view.addSubview(imageIconLeftView)
self.inputTextField.leftView = view
self.inputTextField.leftViewMode = .always

I have a button for toggle it but I have no idea to do this.

func hideIcon() {
    /// code for hide textfield's leftView
}

Can someone suggest some idea about it?.


Solution

  • In your hideIcon method, try self.inputTextField.leftView = nil and don't forget to call your hideIcon method.