Skip to content

Instantly share code, notes, and snippets.

@yabenatti yabenatti/ReusableView.h
Last active Jun 15, 2017

Embed
What would you like to do?
ReusableView
#import <UIKit/UIKit.h>
IB_DESIGNABLE //Reusable Views loaded from a xib file, you can see them on storyboard
@interface ReusableView : UIView
@property (weak, nonatomic) IBOutlet UILabel *message;
@end
#import "EmptyStateView.h"
@implementation EmptyStateView
#pragma mark - init methods
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// load view frame XIB
[self commonSetup];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
// load view frame XIB
[self commonSetup];
}
return self;
}
#pragma mark - setup view
- (UIView *)loadViewFromNib {
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
// An exception will be thrown if the xib file with this class name not found,
UIView *view = [[bundle loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] firstObject];
return view;
}
- (void)commonSetup {
UIView *nibView = [self loadViewFromNib];
nibView.frame = self.bounds;
// the autoresizingMask will be converted to constraints, the frame will match the parent view frame
nibView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// Adding nibView on the top of our view
[self addSubview:nibView];
}
- (void)awakeFromNib {
[super awakeFromNib];
}
@end
/* Set the file owner as the .h file above */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.