简体   繁体   English

UITextField如何添加清除按钮?

[英]How to add clear button to UITextField?

Same as Adding the "Clear" Button to an iPhone UITextField but that one is old.将“清除”按钮添加到 iPhone UITextField 相同,但该按钮已旧。 I tried我试过了

myUITextField.clearButtonMode = UITextFieldViewModeWhileEditing;

but nothing is happening.但什么也没发生。 What could be preventing it?什么可以阻止它?

In cellForRowAtIndex, the fix looks like this in my case:在 cellForRowAtIndex 中,在我的情况下,修复看起来像这样:

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;
    }

In some situations the UITextField height and alignment can cause the clear button to be hidden below the cell even though your text input displays fine.在某些情况下,UITextField 高度和 alignment 会导致清除按钮隐藏在单元格下方,即使您的文本输入显示正常。

Double check your height or try this:仔细检查你的身高或试试这个:

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM