首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Autolayout编程

Autolayout编程
EN

Stack Overflow用户
提问于 2015-10-26 12:44:52
回答 3查看 540关注 0票数 0

我试图深入到汽车布局的深度,我对storyboard.Also的汽车布局有很好的了解,我也知道如何使用NSLayoutConstraint类。问题是:我有两个视图( redView和yellowView).From故事板,我已经在代码中设置了两个views.Now的约束,假设我想更改redView w.r.t的宽度-- yellowView.So的宽度--为此我使用了以下代码:

代码语言:javascript
复制
NSLayoutConstraint *layouts1 = [NSLayoutConstraint 
                               constraintWithItem:_redView 
                               attribute:NSLayoutAttributeWidth 
                               relatedBy:NSLayoutRelationEqual 
                               toItem:_yellowView 
                               attribute:NSLayoutAttributeWidth
                               multiplier:3.0f 
                               constant:0];
[self.view addConstraint:layouts1]; 

现在我运行代码,虽然我得到了预期的输出,但它在控制台上显示了“不可满足的约束”消息(因为设置了多个宽度),.Now的问题是:我如何处理掉该消息?我尝试过几件事,但它们不是working.Here,这是我尝试过的:

  1. 我没有在storyboard.Instead中设置约束,我直接使用了下面的代码。
  2. 我试着用‘布局’的方法。

嗯,我可以用编程的方式编写整个自动布局代码,但是由于我们有通过故事板设置自动收费的特权,所以unnecessary.There必须是完全以某种方式以编程方式更新约束(设置在故事板中),而不产生任何冲突。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-10-26 13:12:00

好吧,我明白我想要做的是什么--我已经在故事板中设置了约束,我需要删除这个宽度约束(在故事板中),用一个新的do.As更新它--我刚刚创建了一个与constraint.SO宽度相同的IBOutlet,然后这段代码帮了我的忙:

代码语言:javascript
复制
[_yellowView removeConstraint:_myConstraints];//_myConstraints is the outlet

现在我没有面对任何problem.Thanks很多人:-)

票数 0
EN

Stack Overflow用户

发布于 2015-10-27 10:44:45

当您想以编程方式编辑现有约束时,我是否正确地理解了您?即从故事板中设置的约束?

可以从约束创建IBOutlet,并以编程方式设置其constant-property。然后打电话给layotIfNeeded

票数 1
EN

Stack Overflow用户

发布于 2015-10-27 12:21:51

标记:-查找约束

代码语言:javascript
复制
- (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;
}

标记:-删除约束

代码语言:javascript
复制
- (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;
}

标记:-大小约束

代码语言:javascript
复制
- (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];
}

马克:-中心违禁品

代码语言:javascript
复制
- (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];
}

标记:-边缘附加约束

代码语言:javascript
复制
- (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];
}

标记:-对不同边缘的边缘约束

代码语言:javascript
复制
- (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];
}

标记:-尺寸附加约束

代码语言:javascript
复制
- (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"];
}

标记:-行&列布局约束

代码语言:javascript
复制
- (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];
}

标记:-行&列等尺寸布局约束

代码语言:javascript
复制
- (void)addEqualSizeLayoutConstraintsForMySubviews:(NSArray *)views
                                       firstOffset:(CGFloat)firstOffset
                                      secondOffset:(CGFloat)secondOffset
                                     betweenOffset:(NSString *)betweenOffset
                                         direction:(NSString *)direction
{
    /* 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)
         {
             if (betweenOffset)
             {
                 [visualFormatString appendFormat:@"[%@]-%@-", viewName, betweenOffset];
             }
             else
             {
                 [visualFormatString appendFormat:@"[%@(>=40)]", viewName];
             }
         }
         else
         {
             [visualFormatString appendFormat:@"[%@(>=40)]-%.0f-|", viewName, secondOffset];
         }
     }];


    //    DebugLog(@"viewDict %@", viewDict);

    /* Add constraints */
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[view0]-2-[view1(==view0)]-2-[view2(==view1)]-2-[view3(==view2)]-2-[view4(==view3)]-2-[view5(==view4)]-0-|"
                                                                   options:0
                                                                   metrics:0
                                                                     views:viewDict];
    [self addConstraints:constraints];
}


- (void)addRowLayoutEqualWidthConstraintsForMySubviews:(NSArray *)subviews
                                            leftOffset:(CGFloat)leftOffset
                                           rightOffset:(CGFloat)rightOffset
                                         betweenOffset:(NSString *)betweenOffset
{
    [self addEqualSizeLayoutConstraintsForMySubviews:subviews
                                         firstOffset:leftOffset
                                        secondOffset:rightOffset
                                       betweenOffset:betweenOffset
                                           direction:@"H"];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33346109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档