Skip to content

Instantly share code, notes, and snippets.

@yabenatti
Last active June 15, 2017 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yabenatti/6d11a6b348ccb9c610f156ba5303bb5b to your computer and use it in GitHub Desktop.
Save yabenatti/6d11a6b348ccb9c610f156ba5303bb5b to your computer and use it in GitHub Desktop.
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