- IB_DESIGNABLE 让你的自定 UIView 可以在 IB 中预览。
- IBInspectable 让你的自定义 UIView 的属性出现在 IB 中 Attributes inspector 。
缺点:
- 无法直接在UIViewController上面使用 IB_DESIGNABLE,只能用于UIView的子类。
- 渲染可能会出错,有时候比较慢。
在自定义的TestView中 加上 IB_DESIGNABLE 修饰
即可以在Xib中看到看到相应效果
IB_DESIGNABLE
IB_DESIGNABLE
@interface TestView : UIView
...
@end
下面两种方法初始化的视图都可以即时看到效果
- (void)drawRect:(CGRect)rect {
}
或Masonry布局
当从代码实例化UIView的时候,initWithFrame会执行;
当从文件加载UIView的时候,initWithCoder会执行
==========================================
==========================================
-(instancetype)initWithCoder:(NSCoder *)coder{
if (self = [super initWithCoder:coder]) {
[self setupView];
}
return self;
}
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setupView];
}
return self;
}
-(void)setupView{
self.backgroundColor = UIColor.redColor;
//背景
_backgroundImageView = ({
UIImageView *imageView = [[UIImageView alloc]init];
imageView.image = [UIImage imageForIBWithImageNamed:@"bg" classForBundle:[self class]];
[self addSubview:imageView];
imageView;
});
//未来
_futurelabel = ({
UILabel *label = [[UILabel alloc]init];
label.font = [UIFont systemFontOfSize:15];
label.textColor = UIColorFromRGB(0x1ca4a4);
label.text = @"未来";
[self addSubview:label];
label;
});
[self layout];
}
-(void)layout{
__weak __typeof(self) weakSelf = self;
//背景
[self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(weakSelf);
make.center.mas_equalTo(weakSelf);
}];
[_futurelabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(weakSelf);
make.bottom.mas_equalTo(weakSelf.mas_centerY);
}];
}
IBInspectable
在自定义的神图属性前加 IBInspectable
即可以在Xib中实时可视化地更改属性
@interface Testview : UIView
@property (nonatomic, assign) IBInspectable CGFloat cornerRadius;
@property (nonatomic, strong) IBInspectable UIImage * image;
@end
在.m文件上添加set方法,即可以实时更改
#import "Testview.h"
@implementation Testview
- (void)setCornerRadius:(CGFloat) cornerRadius {
_cornerRadius = cornerRadius;
self.layer.cornerRadius = cornerRadius;
self.layer.masksToBounds = YES;
}
@end
在项目中可以作一个基类
IB_DESIGNABLE
/// Xib 自定义View时
/// 可以快速的设置圆角边线颜色宽度
///阴影
@interface EWXibView : UIView
@property (nonatomic, assign) IBInspectable UIColor * ew_layerColor;
@property (nonatomic, assign) IBInspectable CGFloat ew_layerWidth;
@property (nonatomic, assign) IBInspectable CGFloat ew_layerRadius;
@property (nonatomic, assign) IBInspectable CGSize ew_shadowOffset;
@property (nonatomic, assign) IBInspectable CGFloat ew_shadowRadius;
@property (nonatomic, assign) IBInspectable CGFloat ew_shadowOpacity;
@property (nonatomic, assign) IBInspectable UIColor * ew_shadowColor;
@end
@implementation EWXibView
-(void)setEw_layerColor:(UIColor *)LayerColor_ {
self.layer.borderColor = LayerColor_.CGColor;
}
- (UIColor *)ew_layerColor {
return [UIColor colorWithCGColor:self.layer.borderColor];
}
- (void)setEw_layerWidth:(CGFloat)LayerWidth_ {
self.layer.borderWidth = LayerWidth_;
// 解决layer.border.width随着view的放大,会出现锯齿化的问题(iOS7.0)
self.layer.allowsEdgeAntialiasing = YES;
}
- (CGFloat)ew_layerWidth {
return self.layer.borderWidth;
}
- (void)setEw_layerRadius:(CGFloat)LayerRadius_ {
self.layer.cornerRadius = LayerRadius_;
self.layer.masksToBounds = YES;
}
- (CGFloat)ew_layerRadius {
return self.layer.cornerRadius;
}
- (void)setEw_shadowOffset:(CGSize)ShadowOffset_ {
self.layer.shadowOffset = ShadowOffset_;
}
- (CGSize)ew_shadowOffset {
return self.layer.shadowOffset;
}
- (void)setEw_shadowRadius:(CGFloat)ShadowRadius_ {
self.layer.shadowRadius = ShadowRadius_;
}
- (CGFloat)ew_shadowRadius {
return self.layer.shadowRadius;
}
- (void)setEw_shadowOpacity:(CGFloat)ShadowOpacity_ {
self.layer.shadowOpacity = ShadowOpacity_;
}
- (CGFloat)ew_shadowOpacity {
return self.layer.shadowOpacity;
}
- (void)setEw_shadowColor:(UIColor *)ShadowColor_ {
self.layer.shadowColor = ShadowColor_.CGColor;
}
- (UIColor *)ew_shadowColor {
return [UIColor colorWithCGColor:self.layer.shadowColor];
}
@end
网友评论