Custom View using xib
Create Custom View
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
UIView *view = [[[NSBundle bundleForClass:[self class]]loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] firstObject];
view.frame = self.bounds;
[self addSubview:view];
}
return self;
}Try it!
Preview Custom View in Interface Builder (IBDesignable)
1. add IB_DESIGNABLE marco in xxx.h
IB_DESIGNABLE marco in xxx.h2. implement prepareForInterfaceBuilder and load the Custom View from xib like initWithCoder:
prepareForInterfaceBuilder and load the Custom View from xib like initWithCoder:Output


Points to note
1. [NSBundle bundleForClass:] vs [NSBundle mainBundle]
[NSBundle bundleForClass:] vs [NSBundle mainBundle]
2. Other init methods
3. Don't set the Custom Class of the root UIView of the Custom View
4. What's going on?
References
Last updated