ios 自定义 UITextField 清除按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9239328/
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
Custom UITextField clear button
提问by gemini
Is it possible to customize the image of the clear button in a UITextField
? I have a dark textfield background and the "x" is not visible enough.
是否可以在 a 中自定义清除按钮的图像UITextField
?我有一个深色的文本字段背景,“x”不够明显。
回答by Danra
You can set your own custom clear button to the text field's rightView
property. Make sure set the rightViewMode
property to UITextFieldViewModeWhileEditing
or UITextFieldViewModeAlways
, whatever makes sense for your case.
您可以将自己的自定义清除按钮设置为文本字段的rightView
属性。确保将rightViewMode
属性设置为UITextFieldViewModeWhileEditing
or UITextFieldViewModeAlways
,无论对您的情况是否有意义。
If the button's position isn't right for your need, you can subclass UITextField
and override the rightViewRectForBounds:
method.
如果按钮的位置不适合您的需要,您可以子类化UITextField
并覆盖该rightViewRectForBounds:
方法。
The documentation says the default for the clearButtonMode
property is UITextFieldViewModeNever
, but I suspect Interface Builder may set it to UITextFieldViewModeWhileEditing
- Make sure you set it to the "never" value so it doesn't appear.
文档说该clearButtonMode
属性的默认值是UITextFieldViewModeNever
,但我怀疑 Interface Builder 可能会将其设置为UITextFieldViewModeWhileEditing
- 确保将其设置为“从不”值,这样它就不会出现。
All these properties are documented in UITextField Class Reference.
所有这些属性都记录在UITextField 类参考中。
回答by Laszlo
You can access that property using KVO:
您可以使用 KVO 访问该属性:
UIButton *clearButton = [myTextField valueForKey:@"_clearButton"];
[clearButton setImage:[UIImage new] forState:UIControlStateNormal];
回答by Brody Robertson
Tested iOS7.0 - 8.4
已测试 iOS7.0 - 8.4
It is possible to update the backgroundImage of all clear buttons using the following UIAppearance method. Unfortunately you cannot update the image of the button:
可以使用以下 UIAppearance 方法更新所有清除按钮的 backgroundImage。不幸的是,您无法更新按钮的图像:
UIButton *defaultClearButton = [UIButton appearanceWhenContainedIn:[UITextField class], nil];
[defaultClearButton setBackgroundImage:[UIImage imageNamed:@"clearButtonBkg1.png"] forState:UIControlStateNormal];
Using the following images, this results in the following clear button:
使用以下图像,会产生以下清除按钮:
White background image @3x:
白色背景图片@3x:
Note: This will update the background image of ALL buttons contained in textfields
注意:这将更新文本字段中包含的所有按钮的背景图像
回答by zgorawski
Swift 2.2+ / iOS 9+ version of @Brody Robertson's answer:
Swift 2.2+ / iOS 9+ 版本@Brody Robertson 的回答:
let defaultClearButton = UIButton.appearanceWhenContainedInInstancesOfClasses([UITextField.self])
defaultClearButton.setBackgroundImage(UIImage(named: "ClearIcon"), forState: UIControlState.Normal)
回答by Claus
Swift 3
斯威夫特 3
let customClearButton = UIButton.appearance(whenContainedInInstancesOf: [UITextField.self])
customClearButton.setBackgroundImage(UIImage(named: "custom_cb"), for: .normal)
回答by Sebastian Wramba
One approach would be to create a custom UITextField
that has your desired image as a subview and clears the parent text field when tapped. You disable the default button and add your custom behavior in this custom text field.
一种方法是创建一个自定义UITextField
,将您想要的图像作为子视图,并在点击时清除父文本字段。您禁用默认按钮并在此自定义文本字段中添加自定义行为。
This should get you started. If there are any more concrete questions, feel free to edit your question or comment here if it's a minor problem.
这应该让你开始。如果有任何更具体的问题,如果这是一个小问题,请随时编辑您的问题或在此处发表评论。
回答by Pavel Volobuev
depends on @Tuss László's answer
取决于@Tuss László 的回答
Swift 3.0
斯威夫特 3.0
myTextField.clearButtonMode = .whileEditing
if let clearButton = myTextField.value(forKeyPath: "_clearButton") as? UIButton {
clearButton.setImage(UIImage(named:"myImage"), for: .normal)
clearButton.setImage(UIImage(named:"myImage"), for: .highlighted)
}
But really use it carefully. If Apple change implementations in future iOS releases, it would not be work
但是真的要慎重使用。如果 Apple 在未来的 iOS 版本中更改实现,它将行不通