Skip to content

Instantly share code, notes, and snippets.

@ranmyfriend
Last active August 29, 2015 14:26
Show Gist options
  • Save ranmyfriend/3d31b48b2e5f361cc0d2 to your computer and use it in GitHub Desktop.
Save ranmyfriend/3d31b48b2e5f361cc0d2 to your computer and use it in GitHub Desktop.
Layout Issue
- (void)addConstraints {
[self.view removeConstraints:self.view.constraints];
NSDictionary *views = NSDictionaryOfVariableBindings(_contactImageProperty,_peopleAllowLabelProperty,_shareCodesDefaultMessageLabelProperty,_continueBtnProperty);
_contactImageProperty.translatesAutoresizingMaskIntoConstraints = NO;
_peopleAllowLabelProperty.translatesAutoresizingMaskIntoConstraints = NO;
_shareCodesDefaultMessageLabelProperty.translatesAutoresizingMaskIntoConstraints = NO;
_continueBtnProperty.translatesAutoresizingMaskIntoConstraints = NO;
// contactImageProperty
NSArray *constraints = [NSArray arrayWithObject:[NSLayoutConstraint constraintWithItem:_contactImageProperty attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0f]];
constraints = [constraints arrayByAddingObject:[NSLayoutConstraint constraintWithItem:_contactImageProperty attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:200]];
constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[_contactImageProperty]" options:0 metrics:nil views:views]];
// peopleAllowLabelProperty
constraints = [constraints arrayByAddingObject:[NSLayoutConstraint constraintWithItem:_peopleAllowLabelProperty attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
constraints = [constraints arrayByAddingObject:[NSLayoutConstraint constraintWithItem:_peopleAllowLabelProperty attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:50]];
constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_contactImageProperty]-10-[_peopleAllowLabelProperty]" options:0 metrics:nil views:views]];
// shareCodesDefaultMessageLabelProperty
constraints = [constraints arrayByAddingObject:[NSLayoutConstraint constraintWithItem:_shareCodesDefaultMessageLabelProperty attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
constraints = [constraints arrayByAddingObject:[NSLayoutConstraint constraintWithItem:_shareCodesDefaultMessageLabelProperty attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:100]];
constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_shareCodesDefaultMessageLabelProperty]-|" options:0 metrics:nil views:views]];
constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_peopleAllowLabelProperty]-100-[_shareCodesDefaultMessageLabelProperty]" options:0 metrics:nil views:views]];
// continueBtnProperty
constraints = [constraints arrayByAddingObject:[NSLayoutConstraint constraintWithItem:_continueBtnProperty attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
constraints = [constraints arrayByAddingObject:[NSLayoutConstraint constraintWithItem:_continueBtnProperty attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:50]];
constraints = [constraints arrayByAddingObject:[NSLayoutConstraint constraintWithItem:_continueBtnProperty attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:300]];
constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_continueBtnProperty]-10-|" options:0 metrics:nil views:views]];
[self.view addConstraints:constraints];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
NSDictionary *views = NSDictionaryOfVariableBindings(_peopleAllowLabelProperty,_shareCodesDefaultMessageLabelProperty,_continueBtnProperty);
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_contactImageProperty attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:200.0f]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_peopleAllowLabelProperty]-200-[_shareCodesDefaultMessageLabelProperty]" options:0 metrics:nil views:views]];
}
else {
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_contactImageProperty attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:100.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_shareCodesDefaultMessageLabelProperty attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:50]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_peopleAllowLabelProperty]-20-[_shareCodesDefaultMessageLabelProperty]-[_continueBtnProperty]" options:0 metrics:nil views:views]];
}
[self.view setNeedsLayout];
[self.view setNeedsUpdateConstraints];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment