Skip to content

Instantly share code, notes, and snippets.

@dymx101
Created August 10, 2014 09:02
Show Gist options
  • Save dymx101/fd26ac336942ed4307b7 to your computer and use it in GitHub Desktop.
Save dymx101/fd26ac336942ed4307b7 to your computer and use it in GitHub Desktop.
Customize UITextField Clear Button Style
You'll need to create your own clear button image in this case. I would suggest taking a screenshot of the clear button and editing in photoshop.
You can take that image and create a UIButton with the image dimensions. From there you can set it as the UITextField's rightView. Like so:
================================================================
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"clear_button.png"] forState:UIControlStateNormal];
[button setFrame:CGRectMake(0.0f, 0.0f, 15.0f, 15.0f)]; // Required for iOS7
theTextField.rightView = button;
theTextField.rightViewMode = UITextFieldViewModeWhileEditing;
================================================================
I typed that without syntax checking and what not so you'll want to check it out before running it. You'll also want to replace clear_button.png with whatever your image name is.
You'll also need to write your own method to clear the text field.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment