在开发过程中,自定义控件使用的频率非常高,接下来简单对自定义控件做个小结
关于layoutSubviews在以下情况下会被调用:
- init初始化不会触发layoutSubviews
- addSubview会触发layoutSubviews
- 设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化
- 滚动一个UIScrollView会触发layoutSubviews
- 旋转Screen会触发父UIView上的layoutSubviews事件
- 改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件
完整的例子
@class ImageData;
@interface ImageDataView : UIView
//模型
@property (nonatomic,strong) ImageData *imageData;
@end
#import "ImageData.h"
@interface ImageDataView()
@property (nonatomic,strong) UIImageView *imageView;
@property (nonatomic,strong) UILabel *label;
@end
@implementation ImageDataView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
}
return self;
}
#pragma mark- 懒加载
- (UIImageView *)imageView{
if (_imageView == nil) {
UIImageView *imageView = [[UIImageView alloc]init];
[imageView setBackgroundColor:[UIColor blueColor]];
[self addSubview:imageView];
_imageView = imageView;
}
return _imageView;
}
- (UILabel *)label{
if (_label == nil) {
UILabel *label = [[UILabel alloc]init];
[label setBackgroundColor:[UIColor redColor]];
label.textAlignment = NSTextAlignmentCenter;
[self addSubview:label];
_label = label;
}
return _label;
}
#pragma mark- 模型赋值
- (void)setImageData:(ImageData *)imageData{
_imageData = imageData;
self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageData.icon]];
self.label.text = imageData.name;
}
#pragma mark- 子控件坐标
//这个方法专门用于布局子控件,一般在这里设置子控件的frame
//当控件本身的尺寸发送改变时,系统会自动调用这个方法
- (void)layoutSubviews{
[super layoutSubviews];
CGFloat personW = self.frame.size.width;
CGFloat personH = self.frame.size.height;
self.imageView.frame = CGRectMake(0, 0, personW, personH-20);
self.label.frame = CGRectMake(0, personH-20, personW, 20);
}
@end
方法调用
ImageData *imageData = [[ImageData alloc]initWithDic:dic];
ImageDataView *view = [[ImageDataView alloc]init];
view.imageData = imageData;
view.frame = CGRectMake(shopX, shopY, imageW, imageH);
[bgView addSubview:view];
创建xib
添加子控件
[[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] lastObject]
完整的例子
#import "ImageDataView.h"
#import "ImageData.h"
@interface ImageDataView()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation ImageDataView
+ (instancetype)shareImageDataView{
return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] lastObject];
}
#pragma mark- 模型赋值
- (void)setImageData:(ImageData *)imageData{
_imageData = imageData;
self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageData.icon]];
self.label.text = imageData.name;
}
@end