我有点想深入了解自动布局。我对从故事板中进行自动布局工作有很好的了解。同时,我也知道如何使用NSLayoutConstraint类。 这是问题:我有2个视图(redView和yellowView)。从故事板,我已经设置了两个视图的约束。现在在我的代码中,假设我想要改变redView的宽度wrt的宽度为yellowView.So我使用了以下代码:
NSLayoutConstraint *layouts1 = [NSLayoutConstraint
constraintWithItem:_redView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:_yellowView
attribute:NSLayoutAttributeWidth
multiplier:3.0f
constant:0];
[self.view addConstraint:layouts1];
现在我运行代码,虽然我得到了预期的输出,但它显示了“不满意的约束”。控制台上的消息(它应该因为设置了多个宽度)。现在的问题是:我怎么能摆脱那条消息? 我尝试了几件事,但他们没有工作。这就是我所尝试的:
我可以通过编程方式编写整个自动布局代码,但由于我们有权通过故事板设置自动布局,因此完全没有必要。必须以某种方式只是以编程方式更新约束(在故事板中设置)而不会产生任何冲突。
答案 0 :(得分:1)
当您想以编程方式编辑现有约束时,我是否正确理解您?即故事板设置的约束?
您可以从约束中创建IBOutlet
,并以编程方式设置其constant
- 属性。然后拨打layotIfNeeded
。
答案 1 :(得分:0)
我明白我想要做什么。由于我已经在故事板中设置了约束,我需要删除宽度约束(在故事板中)以使用新约束更新它。我刚创建了一个IBOutlet我的yellowView的宽度,然后这个代码为我做了诀窍:
[_yellowView removeConstraint:_myConstraints];//_myConstraints is the outlet
现在我没有遇到任何问题。谢谢很多人: - )
答案 2 :(得分:0)
MARK: - 查找约束
false
MARK: - 删除约束
- (NSLayoutConstraint *)myConstraintWithAttribute:(NSLayoutAttribute)attribute
{
/* Find constraint with attribute in my constraints */
__block NSLayoutConstraint *resultConstraint;
[self.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop)
{
// DebugLog(@"constraint %@", constraint);
if ([NSStringFromClass([NSLayoutConstraint class]) isEqualToString:NSStringFromClass([constraint class])])
{
if (constraint.firstAttribute == attribute || constraint.secondAttribute == attribute)
{
resultConstraint = constraint;
*stop = YES;
}
}
}];
return resultConstraint;
}
- (NSLayoutConstraint *)superviewConstraintWithAttribute:(NSLayoutAttribute)attribute
{
/* Find constraint with attribute in my superview's constraints */
__block NSLayoutConstraint *resultConstraint;
[self.superview.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop)
{
if (constraint.firstItem == self && constraint.firstAttribute == attribute)
//|| (constraint.secondItem == self && constraint.secondAttribute == attribute))
{
resultConstraint = constraint;
*stop = YES;
}
}];
return resultConstraint;
}
- (NSLayoutConstraint *)constraintWithAttribute:(NSLayoutAttribute)attribute
{
/* Find constraint with attribute in my constraints */
NSLayoutConstraint *resultConstraint = [self myConstraintWithAttribute:attribute];
/* Find constraint with attribute in my superview's constraints */
if (!resultConstraint)
{
resultConstraint = [self superviewConstraintWithAttribute:attribute];
}
return resultConstraint;
}
- (BOOL)removeConstraintWithAttribute:(NSLayoutAttribute)attribute
{
NSLayoutConstraint *constraint = [self superviewConstraintWithAttribute:attribute];
if (constraint)
{
[self.superview removeConstraint:constraint];
return YES;
}
constraint = [self myConstraintWithAttribute:attribute];
if (constraint)
{
[self removeConstraint:constraint];
return YES;
}
return NO;
}
MARK: - 尺寸限制
- (void)removeMyConstraints
{
/* Remove all my constraitns from superview */
[self.superview removeConstraints:[self mySuperviewConstraints]];
/* Remove my constraitns */
[self removeConstraints:self.constraints];
}
- (NSArray *)mySuperviewConstraints
{
NSMutableArray *mySuperviewConstraints = [NSMutableArray array];
[self.superview.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop)
{
if (constraint.firstItem == self || constraint.secondItem == self)
{
[mySuperviewConstraints addObject:constraint];
}
}];
return mySuperviewConstraints;
}
- (void)removeMyConstraintsButKeepMySubviewConstraints
{
/* Remove all my constraitns from superview */
[self.superview removeConstraints:[self mySuperviewConstraints]];
/* Remove my constraitns */
[self removeConstraints:[self myConstraints]];
}
- (NSArray *)myConstraints
{
NSMutableArray *myConstraints = [NSMutableArray array];
[self.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop)
{
if (constraint.firstItem == self && constraint.secondItem == nil)
{
[myConstraints addObject:constraint];
}
}];
return myConstraints;
}
MARK: - 中心限制
- (void)addWidthConstraint:(CGFloat)width
{
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1
constant:width];
[self addConstraint:constraint];
}
- (void)addWidthConstraintFromLabel:(UILabel *)label
withOffset:(CGFloat)offset
{
NSDictionary *attributes = @{NSFontAttributeName : label.font};
return [self addWidthConstraint:[label.text sizeWithAttributes:attributes].width + offset];
}
- (void)addHeightConstraint:(CGFloat)height
{
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1
constant:height];
[self addConstraint:constraint];
}
- (void)addMaximumHeightConstraint:(CGFloat)maxHeight
{
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:nil
attribute:0
multiplier:1
constant:maxHeight];
[self addConstraint:constraint];
}
- (void)addWidthConstraintFromImage:(UIImage *)image
{
[self addWidthConstraint:image.size.width];
}
- (void)addHeightConstraintFromImage:(UIImage *)image
{
[self addHeightConstraint:image.size.height];
}
MARK: - Edge Attach Constraints
- (void)addCenterConstraint:(UIView *)view
centerDirection:(NSLayoutAttribute)centerDirection
offset:(CGFloat)offset
{
UIView *viewItem = (view) ? view : self.superview;
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self
attribute:centerDirection
relatedBy:NSLayoutRelationEqual
toItem:viewItem
attribute:centerDirection
multiplier:1
constant:offset];
[self.superview addConstraint:constraint];
}
- (void)addCenterXConstraint:(UIView *)view
{
[self addCenterConstraint:view
centerDirection:NSLayoutAttributeCenterX
offset:0];
}
- (void)addCenterYConstraint:(UIView *)view
{
[self addCenterConstraint:view
centerDirection:NSLayoutAttributeCenterY
offset:0];
}
- (void)addCenterXConstraint:(UIView *)view
offset:(CGFloat)offset
{
[self addCenterConstraint:view
centerDirection:NSLayoutAttributeCenterX
offset:offset];
}
- (void)addCenterYConstraint:(UIView *)view
offset:(CGFloat)offset
{
[self addCenterConstraint:view
centerDirection:NSLayoutAttributeCenterY
offset:offset];
}
MARK: - 边缘约束到不同边缘
- (void)addEdgeAttachConstraint:(UIView *)view
viewEdge:(NSLayoutAttribute)viewLayoutAttribute
offset:(CGFloat)offset
edge:(NSLayoutAttribute)layoutAttribute
{
UIView *viewItem = (view) ? view : self.superview;
/* Reverse offset for right side and bottom */
CGFloat fixedOffset = offset;
if (layoutAttribute == NSLayoutAttributeRight
|| layoutAttribute == NSLayoutAttributeBottom
|| layoutAttribute == NSLayoutAttributeTrailing)
{
fixedOffset = -offset;
}
/* Add contraint */
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self
attribute:layoutAttribute
relatedBy:NSLayoutRelationEqual
toItem:viewItem
attribute:viewLayoutAttribute
multiplier:1
constant:fixedOffset];
[self.superview addConstraint:constraint];
}
- (void)addLeftEdgeAttachConstraint:(UIView *)view
offset:(CGFloat)offset
{
[self addEdgeAttachConstraint:view
viewEdge:NSLayoutAttributeLeft
offset:offset
edge:NSLayoutAttributeLeft];
}
- (void)addRightEdgeAttachConstraint:(UIView *)view
offset:(CGFloat)offset
{
[self addEdgeAttachConstraint:view
viewEdge:NSLayoutAttributeRight
offset:offset
edge:NSLayoutAttributeRight];
}
- (void)addTopEdgeAttachConstraint:(UIView *)view
offset:(CGFloat)offset
{
[self addEdgeAttachConstraint:view
viewEdge:NSLayoutAttributeTop
offset:offset
edge:NSLayoutAttributeTop];
}
- (void)addBottomEdgeAttachConstraint:(UIView *)view
offset:(CGFloat)offset
{
[self addEdgeAttachConstraint:view
viewEdge:NSLayoutAttributeBottom
offset:offset
edge:NSLayoutAttributeBottom];
}
- (void)addLeftEdgeAttachConstraint:(UIView *)view
{
[self addLeftEdgeAttachConstraint:view
offset:0];
}
- (void)addRightEdgeAttachConstraint:(UIView *)view
{
[self addRightEdgeAttachConstraint:view
offset:0];
}
- (void)addTopEdgeAttachConstraint:(UIView *)view
{
[self addTopEdgeAttachConstraint:view
offset:0];
}
- (void)addBottomEdgeAttachConstraint:(UIView *)view
{
[self addBottomEdgeAttachConstraint:view
offset:0];
}
- (void)addEdgeAttachConstraints:(UIView *)view
leftOffset:(CGFloat)leftOffset
rightOffset:(CGFloat)rightOffset
topOffset:(CGFloat)topOffset
bottomOffset:(CGFloat)bottomOffset
{
[self addLeftEdgeAttachConstraint:view
offset:leftOffset];
[self addRightEdgeAttachConstraint:view
offset:rightOffset];
[self addTopEdgeAttachConstraint:view
offset:topOffset];
[self addBottomEdgeAttachConstraint:view
offset:bottomOffset];
}
- (void)addEdgeAttachConstraints:(UIView *)view
{
[self addLeftEdgeAttachConstraint:view];
[self addRightEdgeAttachConstraint:view];
[self addTopEdgeAttachConstraint:view];
[self addBottomEdgeAttachConstraint:view];
}
MARK: - Size Attach Constaints
- (void)addLeftEdgeAttachConstraint:(UIView *)view
viewEdge:(NSLayoutAttribute)viewLayoutAttribute
offset:(CGFloat)offset
{
[self addEdgeAttachConstraint:view
viewEdge:viewLayoutAttribute
offset:offset
edge:NSLayoutAttributeLeft];
}
- (void)addRightEdgeAttachConstraint:(UIView *)view
viewEdge:(NSLayoutAttribute)viewLayoutAttribute
offset:(CGFloat)offset
{
[self addEdgeAttachConstraint:view
viewEdge:viewLayoutAttribute
offset:offset
edge:NSLayoutAttributeRight];
}
- (void)addTopEdgeAttachConstraint:(UIView *)view
viewEdge:(NSLayoutAttribute)viewLayoutAttribute
offset:(CGFloat)offset
{
[self addEdgeAttachConstraint:view
viewEdge:viewLayoutAttribute
offset:offset
edge:NSLayoutAttributeTop];
}
- (void)addBottomEdgeAttachConstraint:(UIView *)view
viewEdge:(NSLayoutAttribute)viewLayoutAttribute
offset:(CGFloat)offset
{
[self addEdgeAttachConstraint:view
viewEdge:viewLayoutAttribute
offset:offset
edge:NSLayoutAttributeBottom];
}
MARK: - Row&列布局约束
- (void)addSizeAndSuperviewAttachConstraints:(NSString *)sizeConstraint
firstOffset:(CGFloat)firstOffset
secondOffset:(CGFloat)secondOffset
direction:(NSString *)direction
{
NSDictionary *viewDict = NSDictionaryOfVariableBindings(self);
NSString *visualFormatString;
if (sizeConstraint)
{
visualFormatString = [NSString stringWithFormat:@"%@:|-%f-[self(%@)]-%f-|", direction, firstOffset, sizeConstraint, secondOffset];
}
else
{
visualFormatString = [NSString stringWithFormat:@"%@:|-%f-[self]-%f-|", direction, firstOffset, secondOffset];
}
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:visualFormatString
options:0
metrics:0
views:viewDict];
[self.superview addConstraints:constraints];
}
- (void)addWidthAndSuperviewAttachConstraints:(NSString *)widthConstraint
leftOffset:(CGFloat)leftOffset
rightOffset:(CGFloat)rightOffset
{
[self addSizeAndSuperviewAttachConstraints:widthConstraint
firstOffset:leftOffset
secondOffset:rightOffset
direction:@"H"];
}
- (void)addHeightAndSuperviewAttachConstraints:(NSString *)heightConstraint
topOffset:(CGFloat)topOffset
bottomOffset:(CGFloat)bottomOffset
{
[self addSizeAndSuperviewAttachConstraints:heightConstraint
firstOffset:topOffset
secondOffset:bottomOffset
direction:@"V"];
}
MARK: - Row&列等大小布局约束
- (void)addLayoutConstraintsForMySubviews:(NSArray *)views
firstOffset:(CGFloat)firstOffset
secondOffset:(CGFloat)secondOffset
betweenOffset:(NSString *)betweenOffset
direction:(NSString *)direction
equalSize:(BOOL)equalSize
{
/* Create viewDict and visualFormatString */
NSMutableString *visualFormatString = [[NSMutableString alloc] initWithFormat:@"%@:|-%.0f-", direction, firstOffset];
NSMutableDictionary *viewDict = [[NSMutableDictionary alloc] init];
[views enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop)
{
NSString *viewName = [NSString stringWithFormat:@"view%i", idx];
[viewDict setObject:view
forKey:viewName];
if (idx < [views count] - 1)
{
/* Add each view */
if (betweenOffset) /* Add offset between view */
{
/* Add equal size to prev view for all but index 0 */
if (equalSize && idx > 0)
{
NSString *prevViewName = [NSString stringWithFormat:@"view%i", idx - 1];
[visualFormatString appendFormat:@"[%@(==%@)]-%@-", viewName, prevViewName, betweenOffset];
}
else
{
[visualFormatString appendFormat:@"[%@]-%@-", viewName, betweenOffset];
}
}
else /* No offset between views */
{
/* Add equal size to prev view for all but index 0 */
if (equalSize && idx > 0)
{
NSString *prevViewName = [NSString stringWithFormat:@"view%i", idx - 1];
[visualFormatString appendFormat:@"[%@(==%@)]", viewName, prevViewName];
}
else
{
[visualFormatString appendFormat:@"[%@]", viewName];
}
}
}
else
{
/* Add equal size to prev view for all but index 0 */
if (equalSize && idx > 0)
{
NSString *prevViewName = [NSString stringWithFormat:@"view%i", idx - 1];
[visualFormatString appendFormat:@"[%@(==%@)]-%.0f-|", viewName, prevViewName, secondOffset];
}
else
{
[visualFormatString appendFormat:@"[%@]-%.0f-|", viewName, secondOffset];
}
}
}];
// DebugLog(@"viewDict %@", viewDict);
// DebugLog(@"visualFormatString %@", visualFormatString);
/* Add constraints */
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:visualFormatString
options:0
metrics:0
views:viewDict];
[self addConstraints:constraints];
}
- (void)addRowLayoutConstraintsForMySubviews:(NSArray *)subviews
leftOffset:(CGFloat)leftOffset
rightOffset:(CGFloat)rightOffset
betweenOffset:(NSString *)betweenOffset
equalWidth:(BOOL)equalWidth
{
[self addLayoutConstraintsForMySubviews:subviews
firstOffset:leftOffset
secondOffset:rightOffset
betweenOffset:betweenOffset
direction:@"H"
equalSize:equalWidth];
}
- (void)addColumnLayoutConstraintsForMySubviews:(NSArray *)subviews
topOffset:(CGFloat)topOffset
bottomOffset:(CGFloat)bottomOffset
betweenOffset:(NSString *)betweenOffset
equalHeight:(BOOL)equalHeight
{
[self addLayoutConstraintsForMySubviews:subviews
firstOffset:topOffset
secondOffset:bottomOffset
betweenOffset:betweenOffset
direction:@"V"
equalSize:equalHeight];
}