Skip to content

Instantly share code, notes, and snippets.

@sdgandhi
Created May 21, 2015 19:41
Show Gist options
  • Save sdgandhi/b3e3d738beb47f6b0476 to your computer and use it in GitHub Desktop.
Save sdgandhi/b3e3d738beb47f6b0476 to your computer and use it in GitHub Desktop.
Setting up UIView subclass from XIB
@property (nonatomic, strong) UIView *view;
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupViewFromNib];
[self setupUI];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setupViewFromNib];
[self setupUI];
}
return self;
}
- (void)setupViewFromNib
{
NSBundle *bundle = [NSBundle bundleForClass:self.class];
UINib *nib = [UINib nibWithNibName:NSStringFromClass(self.class) bundle:bundle];
_view = [nib instantiateWithOwner:self options:nil].firstObject;
_view.frame = self.bounds;
_view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:_view];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment