如何自定义边框宽度的所有UITextField外观



我正在尝试自定义borderWith的所有UITextField外观。

尝试这样的事情。只有前 2 行有所作为。其余线路不起作用?

[[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;

您可以将它应用于从UITextField扩展的类或您想要样式的任何UIControl

1. 创建了一个UITextField extension并添加了以下代码:

- (void)awakeFromNib{
    self.layer.borderColor = [UIColor blueColor].CGColor;
    self.layer.borderWidth = 1.0f;
}

2.1 在代码中创建UITextField

现在,如果您在代码中创建UITextField,请#import UITextField的扩展并创建UITextField

2.2 在Interface Builder中创建UIText字段

如果在Interface Builder内创建UIButton,请选择UITextField,转到Identity Inspector并将创建的extension添加为UITextFieldclass

不幸的是,外观代理在图层上不起作用,以执行您想要的操作,您应该对UITextField进行子类化,并在layoutSubviews方法(或在init中(中进行自定义

最新更新