Skip to content

Instantly share code, notes, and snippets.

@wanewang
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save wanewang/4d36fad70eb92b0a9dec to your computer and use it in GitHub Desktop.

Select an option

Save wanewang/4d36fad70eb92b0a9dec to your computer and use it in GitHub Desktop.
custom view with ability to show xib in another view
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface CustomView : UIView
@end
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self xibSetup];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self xibSetup];
}
return self;
}
- (void)xibSetup {
#if !TARGET_INTERFACE_BUILDER
NSBundle *bundle = [NSBundle mainBundle];
#else
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
#endif
UIView *view = [[bundle loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] firstObject];
view.frame = self.layer.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