【问题标题】:How may i customise all UITextField appearances for borderWidth?如何自定义边框宽度的所有 UITextField 外观?
【发布时间】:2015-02-08 19:44:15
【问题描述】:
我正在尝试为 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;
【问题讨论】:
标签:
ios
objective-c
uitextfield
calayer
appearance
【解决方案1】:
您可以将其应用于从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中创建UITextField
如果您在Interface Builder 中创建UIButton,请选择UITextField,转到Identity Inspector 并将创建的extension 添加为class 的UITextField。
【解决方案2】:
不幸的是,外观代理无法在图层上执行您想要的操作,您应该将 UITextField 子类化并在 layoutSubviews 方法(或 init 中)进行自定义