我想用圆形边框制作应用程序的所有按钮。
但uibutton外观不会改变appdelegate中的图层属性:
[[[UIButton appearance] layer] setMasksToBounds:YES];
[[[UIButton appearance] layer] setCornerRadius:8.0f];
有什么不对?
非常感谢答案 0 :(得分:1)
由于这是一个老问题,但它与之相关,您无法使用外观更改图层属性。有关完整列表,您可以查看this page。
对于UIButton,有效选项为:
//UIView
@property(nullable, nonatomic,copy) UIColor *backgroundColor UI_APPEARANCE_SELECTOR;
//UIButton
@property(nonatomic) UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR;
- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR;
- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR;
- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR;
答案 1 :(得分:0)
如果有人正在寻找一个快速解决此问题的方法,即使在6年后,由于外观无法管理CALayer,您可以对UIView进行扩展以使其与外观兼容,诀窍是在上使用dynamic关键字obj变量,这将迫使swift使用以下实现:
extension UIView {
@objc dynamic var borderColor: UIColor? {
get {
if let color = self.layer.borderColor {
return UIColor(cgColor: color)
} else {
return nil
}
}
set(color) {
self.layer.borderColor = color?.cgColor
}
}
@objc dynamic var borderWidth: NSNumber? {
get { return NSNumber(value: Float(self.layer.borderWidth))}
set(width) {
self.layer.borderWidth = CGFloat(width?.floatValue ?? 0)
}
}
@objc dynamic var cornerRadius: NSNumber? {
get { return NSNumber(value: Float(self.layer.cornerRadius))}
set(radius) {
self.layer.cornerRadius = CGFloat(radius?.floatValue ?? 0)
}
}
}