问题
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