用xib 建立一个 自定义view

用xib建立一个自定义的view

核心代码:

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
     self = [super initWithCoder:aDecoder];

     if (self) {
         [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
         self.contentView.frame = self.bounds;
         [self addSubview:self.contentView];
     }

     return self;
 }

load nib的时候,owner为self。
NOTE: load的时机,需要在initWithCoder:或者awakeFromNib里,或者在自己写的convenience initializer里。

错误1 在 ClientListCollectionViewCell 添加几个手势 报错如下:
***

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'invalid nib registered for identifier (ClientListCollectionViewCell) - nib must contain exactly one top level object which must be a UICollectionReusableView instance'

解决方法1:把xib中的手势去掉,在awakeFromNib 添加手势

参考 这里