Last active
April 8, 2019 11:48
-
-
Save isaac-weisberg/9e4fc31101988a77bb784e0acbdf9332 to your computer and use it in GitHub Desktop.
Load your views from xib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// XibbyView.h | |
@interface XibbyView : UIView | |
@property UIView* xibView; | |
@end | |
// XibbyView.m | |
@interface XibbyView () | |
@property UIView* xibView; | |
@end | |
@implementation XibbyView | |
- (instancetype)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) { | |
[self renewXib]; | |
} | |
return self; | |
} | |
- (instancetype)initWithCoder:(NSCoder *)aDecoder | |
{ | |
self = [super initWithCoder:aDecoder]; | |
if (self) { | |
[self renewXib]; | |
} | |
return self; | |
} | |
- (void)renewXib | |
{ | |
if (self.xibView != nil) { | |
[self.xibView removeFromSuperview]; | |
} | |
UIView* view = [[UINib nibWithNibName: NSStringFromClass([self class]) bundle:nil] instantiateWithOwner:self options:nil].firstObject; | |
view.frame = self.bounds; | |
[self addSubview:view]; | |
self.xibView = view; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment