How to add clear button in UITextField?

Same as Adding a “Clear” Button on an iPhone UITextField , but this one is old. I tried

myUITextField.clearButtonMode = UITextFieldViewModeWhileEditing;

but nothing happens. What could be prevented?

+3
source share
2 answers

In cellForRowAtIndex, the fix looks like in my case:

cell = [tableView dequeueReusableCellWithIdentifier:@"CellNameIdentifier"];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"CellName" owner:self options:nil];
        cell = cellName;
        [cellName setAccessoryType:UITableViewCellAccessoryNone];
        //textfield in inside cellName
        textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
+5
source

In some situations, the height and alignment of the UITextField may cause the clear button to be hidden under the cell, even if your text input appears normally.

Double check the height or try the following:

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
0
source

Source: https://habr.com/ru/post/1704942/


All Articles