自定义UITextField清除按钮 [英] Custom UITextField clear button

查看:829
本文介绍了自定义UITextField清除按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 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.

推荐答案

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

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.

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

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

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

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.

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

All these properties are documented in UITextField Class Reference.

这篇关于自定义UITextField清除按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆