1

So far this is my attempt at achieving this.

    if searchBar.text == ""
    {
        searchBar.addSubview(placeholder)

        searchBar.addConstraintsWithFormat("V:|[v0]|", views: placeholder)

        phconstraint = NSLayoutConstraint(item: placeholder, attribute: .centerX, relatedBy: .equal, toItem: searchBar, attribute: .centerX, multiplier: 1, constant: 0)
    }
    else
    {
        placeholder.removeFromSuperview()
    }

The above code best explains the logic behind what I want to achieve. Is there some method in UITextField to detect the addition of text or maybe some notification?

Any suggestions?

rmaddy
  • 307,833
  • 40
  • 508
  • 550
Stefan
  • 868
  • 1
  • 10
  • 28
  • 1
    Does this placeholder view provides any custom functionality than the default placeholder text option? Have you tried using `UITextFieldDelegate`'s method `textFieldDidBeginEditing`? – R P Apr 29 '17 at 03:21

1 Answers1

0

This is the method where you should be writing logic to hide/ show your view:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let currentString: String = (textField.text! as NSString).replacingCharacters(in: range, with: string)
        let length: Int = (currentString.characters.count )

        if(length >= 1){
        //show
        }
        else{
        //hide
        }
       return true
    }
Vamshi Krishna
  • 969
  • 9
  • 18