16

can I change the color of the clearButtonMode on a textField?

theTextField.clearButtonMode = UITextFieldViewModeWhileEditing

shows an x that is grey dark color to delete the textField,

but can i show this button with white color?

thanks

manuelBetancurt
  • 15,428
  • 33
  • 118
  • 216

6 Answers6

33

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.

Daniel Rinser
  • 8,855
  • 4
  • 41
  • 39
Ben Harris
  • 5,664
  • 3
  • 30
  • 27
8

A cleaner way is to implement a category on UITextField with this method:

- (void)modifyClearButtonWithImage:(UIImage *)image {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:image forState:UIControlStateNormal];
    [button setFrame:CGRectMake(0.0f, 0.0f, 15.0f, 15.0f)];
    [button addTarget:self action:@selector(clear:) forControlEvents:UIControlEventTouchUpInside];
    self.rightView = button;
    self.rightViewMode = UITextFieldViewModeWhileEditing;
}

-(IBAction)clear:(id)sender{
    self.text = @"";
}
AcidicSkittles
  • 4,610
  • 1
  • 15
  • 14
mukaissi
  • 2,441
  • 1
  • 21
  • 12
7

Swift version :

extension UITextField {

    func modifyClearButtonWithImage(image : UIImage) {
        let clearButton = UIButton(type: .Custom)
        clearButton.setImage(image, forState: .Normal)
        clearButton.frame = CGRectMake(0, 0, 40, 40)
        clearButton.contentMode = .ScaleAspectFit
        clearButton.addTarget(self, action: #selector(UITextField.clear(_:)), forControlEvents: .TouchUpInside)
        self.rightView = clearButton
        self.rightViewMode = .WhileEditing
    }

    func clear(sender : AnyObject) {
        self.text = ""
    }

}
Julian Fortune
  • 172
  • 1
  • 7
Mohammad Sadiq
  • 5,070
  • 28
  • 29
  • Please don't add the [same answer](http://stackoverflow.com/a/39831522/5292302) to multiple questions. Answer the best one and flag the rest as duplicates. See [Is it acceptable to add a duplicate answer to several questions?](http://meta.stackexchange.com/questions/104227/is-it-acceptable-to-add-a-duplica‌​te-answer-to-several-questions) – Petter Friberg Oct 03 '16 at 12:32
5

Swift 4.1 version

extension UITextField {
    func modifyClearButtonWithImage(image : UIImage) {
        let clearButton = UIButton(type: .custom)
        clearButton.setImage(image, for: .normal)
        clearButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
        clearButton.contentMode = .scaleAspectFit
        clearButton.addTarget(self, action: #selector(UITextField.clear(sender:) ), for: .touchUpInside)
        self.rightView = clearButton
        self.rightViewMode = .whileEditing
    }

    @objc func clear(sender : AnyObject) {
        self.text = ""
        sendActions(for: .editingChanged)
    }
}
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Tony Macaren
  • 437
  • 5
  • 11
  • Works great! If you need a tinted template image, then you can define the tintColor as clearButton.tintColor = yourTintColor and use an image template as described here: https://stackoverflow.com/a/54213781/826946 – Andy Weinstein May 24 '20 at 19:35
  • Actually - this doesn't seem to behave exactly like the built-in cancel button, because it appears even when the textfield is empty (in editing mode, but empty). You need to also take care of setting textField.rightView?.isHidden appropriately to fully emulate standard behavior – Andy Weinstein May 24 '20 at 19:46
3

At the time you're configuring the textField, use:

let clearButton : UIButton = textField.value(forKey: "_clearButton") as! UIButton
let image = UIImage(named: "ClearWhite")

clearButton.setImage(image, for: .normal)
clearButton.backgroundColor = UIColor.clear
mjk
  • 2,443
  • 4
  • 33
  • 33
0

yourTextField.overrideUserInterfaceStyle = .dark

Dian
  • 115
  • 3
  • 12