IOS-自定义View的封装

在开发过程中,自定义控件使用的频率非常高,接下来简单对自定义控件做个小结

View的封装

1:如果一个view内部的子控件比较多,一般会考虑自定义一个view,把它内部的子控件创建封装起来,不让外界看见

2:外界可以传入对应的数据模型给view,view拿到模型数据后给内部子控件设置对应的数据

代码封装

1:新建一个继承UIView的类

2:在initWithFrame:方法中添加子控件(也可以使用懒加载)

3:重写模型属性set方法,在set方法中设置模型属性到子控件上

4:在layoutSubviews方法中设置子控件的frame(一定要调用[super layoutSubviews])

关于layoutSubviews在以下情况下会被调用:

- init初始化不会触发layoutSubviews

- addSubview会触发layoutSubviews

- 设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化

- 滚动一个UIScrollView会触发layoutSubviews

- 旋转Screen会触发父UIView上的layoutSubviews事件

- 改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件

例如:

//自定义view.h文件

@class ImageData;

@interface ImageDataView : UIView

//模型

@property (nonatomic,strong) ImageData *imageData;

@end


//自定义view.m文件

#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];

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容