Tap on UITextField's clear button hides keyboard instead of clearing text

前端 未结 5 1471
[愿得一人]
[愿得一人] 2021-02-07 03:54

In iPhone, I have a view which has a UITextField. When I tap on the clear button of UITextField\'s the keyboard dismissed instead of clearing the text

5条回答
  •  心在旅途
    2021-02-07 04:46

    Just clear the field, resignFirstResponder (if you want to hide keyboard) and return NO/false

    Note: set Attributes inspector property of UITextField

    Clear Button -> Appears while editing

    so it will display the clear button while editing in the text field.

    // Objective-C

    -(BOOL)textFieldShouldClear:(UITextField *)textField
    {
        textField.text = @"";
        [textField resignFirstResponder];
        return NO;
    }
    

    // Swift

    func textFieldShouldClear(textField: UITextField) -> Bool {
        textField.text = ""
        textField.resignFirstResponder()
        return false
    }
    

提交回复
热议问题