阅读 253

xib给视图添加圆角、边框(边框颜色不起作用、无效)

代码设置圆角、边框

// 设置圆角
self.codeCornerRadius.layer.cornerRadius = 5;
self.codeCornerRadius.layer.masksToBounds = YES;

// 设置边框
self.codeBorder.layer.borderWidth = 2;
self.codeBorder.layer.borderColor = [UIColor brownColor].CGColor;
复制代码

xib设置圆角

xib设置边框

方式一:给CALayer增加分类

1、给CALayer增加分类,实现borderUIColor(key path)将UIColor转换成CGColor。 2、xib中设置边框颜色的key path设置成layer.borderUIColor而不是layer.borderColor

@interface CALayer (XibBorderColor)
@end
@implementation CALayer (XibBorderColor)
- (void)setBorderUIColor:(UIColor *)color
{
    self.borderColor = color.CGColor;
}
@end
复制代码

方式二:实现基类

.h文件

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface BaseLayerFeatureXibView : UIView
@property (nonatomic, assign) IBInspectable CGFloat cornerRadius;
@property (nonatomic, assign) IBInspectable CGFloat borderWidth;
@property (nonatomic, assign) IBInspectable UIColor *borderColor;
@property (nonatomic, assign) IBInspectable BOOL    maskToBounds;
@end

NS_ASSUME_NONNULL_END
复制代码

.m文件

#import "BaseLayerFeatureXibView.h"

@implementation BaseLayerFeatureXibView
-(void)setCornerRadius:(CGFloat)cornerRadius {
    _cornerRadius = cornerRadius;
    self.layer.cornerRadius = _cornerRadius;
}
-(void)setBorderColor:(UIColor *)borderColor {
    _borderColor = borderColor;
    self.layer.borderColor = _borderColor.CGColor;
}
-(void)setBorderWidth:(CGFloat)borderWidth {
    _borderWidth = borderWidth;
    self.layer.borderWidth = _borderWidth;
}
-(void)setMaskToBounds:(BOOL)maskToBounds {
    _maskToBounds = maskToBounds;
    self.layer.masksToBounds = _maskToBounds;
}
@end
复制代码

评论