How may i customise all UITextField appearances fo

2019-07-04 04:53发布

问题:

i am trying to customise all UITextField appearances for borderWith.

Trying something like this. Only first 2 lines are making a difference. Rest of the lines not working ?

[[UITextField appearance] setBackgroundColor:[UIColor greenColor]];
[[UITextField appearance] setTextColor:[UIColor blackColor]];
[UITextField appearance].layer.cornerRadius = 6.0f;
[UITextField appearance].layer.borderColor = [UIColor redColor].CGColor;
[UITextField appearance].layer.borderWidth = 3.f;

回答1:

Unfortunately the appearance proxy doesn't work on layers to do what you want you should subclass UITextField and do your customisations in the layoutSubviews method (or in the init)



回答2:

You can apply this to a class extended from UITextField or any UIControl you like to style.

1. Created an extension of UITextField and added following code:

- (void)awakeFromNib{

    self.layer.borderColor = [UIColor blueColor].CGColor;
    self.layer.borderWidth = 1.0f;
}

2.1 Create UITextField inside Code

Now if you create a UITextField inside your code, #import the extension of your UITextField and create the UITextField.

2.2 Create UITextField in Interface Builder

If you create the UIButton inside the Interface Builder, select the UITextField, go to the Identity Inspector and add the created extension as class for the UITextField.