ios 将“清除”按钮添加到 iPhone UITextField

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/320078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:24:54  来源:igfitidea点击:

Adding the "Clear" Button to an iPhone UITextField

ioscocoa-touchuitextfielduikit

提问by Kristopher Johnson

How do you add that little "X" button on the right side of a UITextField that clears the text? I can't find an attribute for adding this sub-control in Interface Builder in the iPhone OS 2.2 SDK.

你如何在 UITextField 的右侧添加那个清除文本的小“X”按钮?我在 iPhone OS 2.2 SDK 的 Interface Builder 中找不到用于添加此子控件的属性。

Note:In Xcode 4.x and later (iPhone 3.0 SDK and later), you can do this in Interface Builder.

注意:在 Xcode 4.x 及更高版本(iPhone 3.0 SDK 及更高版本)中,您可以在 Interface Builder 中执行此操作。

回答by Kristopher Johnson

This button is a built-in overlay that is provided by the UITextFieldclass, but as of the iOS 2.2 SDK, there isn't any way to set it via Interface Builder. You have to enable it programmatically.

这个按钮是一个由UITextField类提供的内置覆盖,但是从 iOS 2.2 SDK 开始,没有任何方法可以通过 Interface Builder 设置它。您必须以编程方式启用它。

Add this line of code somewhere (viewDidLoad, for example):

在某处添加这行代码(viewDidLoad例如):

Objective-C

目标-C

myUITextField.clearButtonMode = UITextFieldViewModeWhileEditing;

Swift 5.0

斯威夫特 5.0

myUITextField.clearButtonMode = .whileEditing

回答by Nicholas Harlen

You can also set this directly from Interface Builder under the Attributes Inspector.

您也可以直接从 Attributes Inspector 下的 Interface Builder 进行设置。

enter image description here

在此处输入图片说明

Taken from XCode 5.1

取自 XCode 5.1

回答by Esqarrouth

Swift 4+:

斯威夫特 4+:

textField.clearButtonMode = UITextField.ViewMode.whileEditing

or even shorter:

甚至更短:

textField.clearButtonMode = .whileEditing

回答by Hossam Ghareeb

you can add custom clear button and control the size and every thing using this:

您可以添加自定义清除按钮并使用此控制大小和所有内容:

UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
[clearButton setImage:img forState:UIControlStateNormal];
[clearButton setFrame:frame];
[clearButton addTarget:self action:@selector(clearTextField:) forControlEvents:UIControlEventTouchUpInside];

textField.rightViewMode = UITextFieldViewModeAlways; //can be changed to UITextFieldViewModeNever,    UITextFieldViewModeWhileEditing,   UITextFieldViewModeUnlessEditing
[textField setRightView:clearButton];

回答by PT Vyas

Objective C :

目标C:

self.txtUserNameTextfield.myUITextField.clearButtonMode = UITextFieldViewModeWhileEditing;

Swift :

斯威夫特:

txtUserNameTextfield.clearButtonMode = UITextField.ViewMode.WhileEditing;

回答by Tritmm

this don't work, do like me:

这不起作用,像我一样:

swift:

迅速:

customTextField.clearButtonMode = UITextField.ViewMode.Always

customTextField.clearsOnBeginEditing = true;

func textFieldShouldClear(textField: UITextField) -> Bool {
    return true
}

回答by Aidan.C

On Xcode 8 (8A218a):

在 Xcode 8 (8A218a) 上:

Swift:

迅速:

textField.clearButtonMode = UITextField.ViewMode.whileEditing;

The "W" went from capital to non-cap "w".

“W”从大写字母变为非大写字母“w”。

回答by Edouard Barbier

Swift 4(adapted from Kristopher Johnson's answer)

Swift 4(改编自克里斯托弗·约翰逊的回答)

textfield.clearButtonMode = .always

textfield.clearButtonMode = .whileEditing

textfield.clearButtonMode = .unlessEditing

textfield.clearButtonMode = .never

回答by Pardeep Kumar

  func clear_btn(box_is : UITextField){
    box_is.clearButtonMode = .always
    if let clearButton = box_is.value(forKey: "_clearButton") as? UIButton {
        let templateImage =  clearButton.imageView?.image?.withRenderingMode(.alwaysTemplate)

        clearButton.setImage(templateImage, for: .normal)
        clearButton.setImage(templateImage, for: .highlighted)

        clearButton.tintColor = .white

     }
}

回答by carlson

On Xcode Version 8.1 (8B62) it can be done directly in Attributes Inspector. Select the textField and then choose the appropriate option from Clear Button drop down box, which is located in Attributes Inspector.

在 Xcode 版本 8.1 (8B62) 上,它可以直接在 Attributes Inspector 中完成。选择 textField,然后从位于属性检查器中的清除按钮下拉框中选择适当的选项。