问题
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
.
来源:https://stackoverflow.com/questions/27403970/how-may-i-customise-all-uitextfield-appearances-for-borderwidth