Last active
April 7, 2016 18:14
-
-
Save thornpig/0ba04aa39111db74de71dbfb8592ff42 to your computer and use it in GitHub Desktop.
Using xib in another xib
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
xib does not automatically unarchive embedded xib. So simply setting a subview's class in IB to another xib's class won't work. | |
Need to manually load the embedded xib in -initWithCoder:. | |
Note that the subview object just loaded needs to be manually added to the view hirerachy. | |
One trick is to have a wrapper view of the subview to be embedded in the prarent xib. | |
The wrapper view's layout can be easily setup in the parent xib. | |
Then in the code, simply add the subview object to the wrapper view in -awakeFromNib: | |
And it's very easy to setup the subview's constraints to take up the wrapper view. | |
In -initWithCoder: the IBOutlets have not been connected, so any IBOutlet property is nil in -initWithCoder: | |
The IBOutlet properties will have been connected to the unarchived view objects in -awakeFromNib: | |
Therefore, the subview object should be added to the view hirerachy in -awakeFromNib:. | |
Constraints of the subview object can be setup in -layoutSubviews. | |
#import "CPSyncIndicatorView.h" | |
@interface CPSyncIndicatorView () | |
@property (nonatomic) IBOutlet UIView *indicatorWrapperView; | |
@end | |
@implementation CPSyncIndicatorView | |
-(void)awakeFromNib | |
{ | |
self.indicator = [[[UINib nibWithNibName: NSStringFromClass([CPActivityIndicator class]) bundle: nil] instantiateWithOwner: nil options: nil] objectAtIndex: 0]; | |
self.indicator.translatesAutoresizingMaskIntoConstraints = NO; | |
[self.indicatorWrapperView addSubview: self.indicator]; | |
} | |
-(void)layoutSubviews | |
{ | |
[super layoutSubviews]; | |
[self.indicatorWrapperView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"H:|[indicator]|" options:0 metrics:nil views: @{@"indicator" : self.indicator}]]; | |
[self.indicatorWrapperView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|[indicator]|" options:0 metrics:nil views: @{@"indicator" : self.indicator}]]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment