Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added autolayout support #2

Merged
merged 1 commit into from
Nov 13, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions UIView+NibLoading.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,36 @@ - (void) loadContentsFromNibNamed:(NSString*)nibName
UIView * containerView = [views objectAtIndex:0];
NSAssert([[containerView class] isEqual:[UIView class]], @"UIView+NibLoading : The container view in nib %@ should be a UIView instead of %@. (It's only a container, and it's discarded after loading.)",nibName,[containerView class]);

[containerView setTranslatesAutoresizingMaskIntoConstraints:NO];

if(CGRectEqualToRect(self.bounds, CGRectZero))
// `self` has no size : use the containerView's size, from the nib file
self.bounds = containerView.bounds;
else
// `self` has a specific size : resize the containerView to this size, so that the subviews are autoresized.
containerView.bounds = self.bounds;

//save constraints for later
NSArray *constraints = containerView.constraints;

// reparent the subviews from the nib file
for (UIView * view in containerView.subviews)
[self addSubview:view];

//re-add constraints, replace containerView with self
for (NSLayoutConstraint *constraint in constraints)
{
id firstItem = constraint.firstItem;
id secondItem = constraint.secondItem;

if (firstItem == containerView)
firstItem = self;

if (secondItem == containerView)
secondItem = self;

[self addConstraint:[NSLayoutConstraint constraintWithItem:firstItem attribute:constraint.firstAttribute relatedBy:constraint.relation toItem:secondItem attribute:constraint.secondAttribute multiplier:constraint.multiplier constant:constraint.constant]];
}
}

- (void) loadContentsFromNib
Expand Down