问题描述
如何在 UITextField 右侧添加用于清除文本的小X"按钮?我在 iPhone OS 2.2 SDK 的 Interface Builder 中找不到用于添加此子控件的属性.
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.
注意:在 Xcode 4.x 及更高版本(iPhone 3.0 SDK 及更高版本)中,您可以在 Interface Builder 中执行此操作.
Note: In Xcode 4.x and later (iPhone 3.0 SDK and later), you can do this in Interface Builder.
推荐答案
这个按钮是由 UITextField
类提供的内置叠加层,但在 iOS 2.2 SDK 中,没有'没有任何方法可以通过 Interface Builder 设置它.您必须以编程方式启用它.
This button is a built-in overlay that is provided by the UITextField
class, 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.
在某处添加这行代码(例如viewDidLoad
):
Add this line of code somewhere (viewDidLoad
, for example):
Objective-C
myUITextField.clearButtonMode = UITextFieldViewModeWhileEditing;
Swift 5.0
myUITextField.clearButtonMode = .whileEditing
这篇关于添加“清除"iPhone UITextField 的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!