Last active
August 29, 2015 14:27
-
-
Save wanewang/4d36fad70eb92b0a9dec to your computer and use it in GitHub Desktop.
custom view with ability to show xib in another view
This file contains hidden or 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
| #import <UIKit/UIKit.h> | |
| IB_DESIGNABLE | |
| @interface CustomView : UIView | |
| @end |
This file contains hidden or 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
| - (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