How to add a custom clear button in UITextField?

随声附和 提交于 2020-01-15 10:33:26

问题


I want to resize the default clear button of 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 S.O, but nothing works for me. These are the links which I referred.

  • Custom UITextField clear button
  • Custom clear button for UITextField
  • Custom clear button in UISearchBar text field

So please suggest some solution which behaves exactly as default clear button of UITextField

Any help will be appreciated.

Thanks in advance.


回答1:


In addition to the jake9115 response, you can emulate the clearbutton behavior by using the UITextFieldDelegate callbacks.

You can try in this way:
Show the button when -textFieldDidBeginEditing: is called
Hide the button when -textFieldDidEndEditing: is called
Hide the button if in -(BOOL)textFieldShouldClear:(UITextField*)textField the length of the textField's text is 0.




回答2:


Why not just make a button that sets the TextView's value to ""?

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


来源:https://stackoverflow.com/questions/16504426/how-to-add-a-custom-clear-button-in-uitextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!