自定义 UITextField 清除按钮

发布于 2025-01-04 18:24:25 字数 72 浏览 2 评论 0原文

是否可以在 UITextField 中自定义清除按钮的图像?我有一个深色的文本字段背景,“x”不够明显。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

风启觞 2025-01-11 18:24:25

您可以将自己的自定义清除按钮设置为文本字段的 rightView 属性。确保将 rightViewMode 属性设置为 UITextFieldViewModeWhileEditingUITextFieldViewModeAlways,无论什么对您的情况有意义。

如果按钮的位置不适合您的需要,您可以继承 UITextField 并重写 rightViewRectForBounds: 方法。

文档说 clearButtonMode 属性的默认值为 UITextFieldViewModeNever,但我怀疑 Interface Builder 可能会将其设置为 UITextFieldViewModeWhileEditing - 确保将其设置为“从不”值,因此它不会出现。

所有这些属性都记录在 UITextField 类参考中。

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.

If the button's position isn't right for your need, you can subclass UITextField and override the rightViewRectForBounds: method.

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.

All these properties are documented in UITextField Class Reference.

奈何桥上唱咆哮 2025-01-11 18:24:25

您可以使用 KVO 访问该属性:

UIButton *clearButton = [myTextField valueForKey:@"_clearButton"];
[clearButton setImage:[UIImage new] forState:UIControlStateNormal];

You can access that property using KVO:

UIButton *clearButton = [myTextField valueForKey:@"_clearButton"];
[clearButton setImage:[UIImage new] forState:UIControlStateNormal];
执笔绘流年 2025-01-11 18:24:25

已测试 iOS7.0 - 8.4

可以使用以下 UIAppearance 方法更新所有透明按钮的背景图像。不幸的是,您无法更新按钮的图像:

UIButton *defaultClearButton = [UIButton appearanceWhenContainedIn:[UITextField class], nil];
[defaultClearButton setBackgroundImage:[UIImage imageNamed:@"clearButtonBkg1.png"] forState:UIControlStateNormal];

使用以下图像,这会产生以下清晰的按钮:

白色背景图像@3x:

“白色透明按钮背景”"

在此处输入图像描述

注意:这将更新文本字段中包含的所有按钮的背景图像

Tested 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:

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:

White clear button background

enter image description here

Note: This will update the background image of ALL buttons contained in textfields

澜川若宁 2025-01-11 18:24:25

斯威夫特3

let customClearButton = UIButton.appearance(whenContainedInInstancesOf: [UITextField.self])
customClearButton.setBackgroundImage(UIImage(named: "custom_cb"), for: .normal)

Swift 3

let customClearButton = UIButton.appearance(whenContainedInInstancesOf: [UITextField.self])
customClearButton.setBackgroundImage(UIImage(named: "custom_cb"), for: .normal)
第几種人 2025-01-11 18:24:25

Swift 2.2+ / iOS 9+ 版本@Brody Robertson 的回答:

let defaultClearButton = UIButton.appearanceWhenContainedInInstancesOfClasses([UITextField.self])
defaultClearButton.setBackgroundImage(UIImage(named: "ClearIcon"), forState: UIControlState.Normal)

Swift 2.2+ / iOS 9+ version of @Brody Robertson's answer:

let defaultClearButton = UIButton.appearanceWhenContainedInInstancesOfClasses([UITextField.self])
defaultClearButton.setBackgroundImage(UIImage(named: "ClearIcon"), forState: UIControlState.Normal)
哆兒滾 2025-01-11 18:24:25

取决于 @Tuss László 的答案

Swift 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)
}

但要小心使用它。如果 Apple 在未来的 iOS 版本中改变实现,那是行不通的

depends on @Tuss László's answer

Swift 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

﹉夏雨初晴づ 2025-01-11 18:24:25

一种方法是创建一个自定义 UITextField,将所需的图像作为子视图,并在点击时清除父文本字段。您禁用默认按钮并在此自定义文本字段中添加自定义行为。

这应该可以帮助您开始。如果有任何更具体的问题,请随时编辑您的问题或在此处发表评论(如果这是一个小问题)。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文