Search code examples
swiftxcodeuitextfielduitextfielddelegate

Disable An Interactive TextField When Clicking On Another TextField


I'm trying to make it so that when I click on one textField and clear it, I can't click on another one. I have this code in my TextFieldShouldReturn function but it does not seem to work. How can I make it so that when I click on one and clear it, the other one is disabled? Tried working out a solution.

if widthPer == textField {
        heightPer.isUserInteractionEnabled = false
        widthPer.isUserInteractionEnabled = true
    } else {
        heightPer.isUserInteractionEnabled = true
        widthPer.isUserInteractionEnabled = false
    }

Solution

  • I used this and it fixes my issue.

      func textFieldShouldClear(_ textField: UITextField) -> Bool {
            if widthPer == textField {
                heightPer.isUserInteractionEnabled = false
                widthPer.isUserInteractionEnabled = true
            } else {
                heightPer.isUserInteractionEnabled = true
                widthPer.isUserInteractionEnabled = false
            }
            return true
        }