UIView子类初始化的隐藏知识

来源:互联网 发布:nginx 静态文件服务器 编辑:程序博客网 时间:2024/06/05 22:45
如果在UIView子类初始化
-(instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        NSLog(@"++++++");        self.backgroundColor = [UIColor whiteColor];    }    return self;}

在使用子类时

    ShareOrederView *shareOrederView = [[ShareOrederView alloc] init];

使用init初始化即调用

-(instancetype)initWithFrame:(CGRect)frame 方法

并自动初始化一个frame,因为此时没传frame,却可以正常显示



反之,如果在UIView子类初始化使用init,在创建子类对象时initWithFrame却不会调子类的init




原创粉丝点击