简体   繁体   English

如何在UITextField中添加自定义清除按钮?

[英]How to add a custom clear button in UITextField?

I want to resize the default clear button of UITextField . 我想调整UITextField的默认清除按钮的大小。 After I googled a lot, I came to know there is no way to modify it. 经过大量的搜索后,我知道无法对其进行修改。

So I decided to go with Custom option ie, by adding UIButton to text field.I found some code from SO, but nothing works for me. 所以我决定使用自定义选项,即在文本字段中添加UIButton 。我从SO中找到了一些代码,但是对我来说没有用。 These are the links which I referred. 这些是我引用的链接。

So please suggest some solution which behaves exactly as default clear button of UITextField 因此,请提出一些行为与UITextField默认清除按钮完全相同的解决方案

Any help will be appreciated. 任何帮助将不胜感激。

Thanks in advance. 提前致谢。

In addition to the jake9115 response, you can emulate the clearbutton behavior by using the UITextFieldDelegate callbacks. 除了jake9115响应之外,您还可以使用UITextFieldDelegate回调来模拟clearbutton行为。

You can try in this way: 您可以通过以下方式尝试:
Show the button when -textFieldDidBeginEditing : is called 调用-textFieldDidBeginEditing :时显示按钮
Hide the button when -textFieldDidEndEditing: is called 调用-textFieldDidEndEditing:时隐藏按钮
Hide the button if in -(BOOL)textFieldShouldClear:(UITextField*)textField the length of the textField's text is 0. 如果在-(BOOL)textFieldShouldClear:(UITextField*)textField中textField的text length为0,则隐藏按钮。

Why not just make a button that sets the TextView's value to ""? 为什么不仅仅制作一个将TextView的值设置为“”的按钮?

- (IBAction)button:(id)sender {
_myTextView.text = "";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM