Getting an existing NSLayoutConstraint for the width?

前端 未结 3 655
自闭症患者
自闭症患者 2021-02-02 14:40

I\'m trying to animate a control in Cocoa with auto layout.

Now, I can set [[constraint animator] setConstant:newWidth];, which works. But how can I get the

3条回答
  •  温柔的废话
    2021-02-02 14:45

    Every contraint has an attribute [constraint firstAttribute] It returns an enum NSLayoutAttribute

    typedef NS_ENUM(NSInteger, NSLayoutAttribute) {
        NSLayoutAttributeLeft = 1,
        NSLayoutAttributeRight,
        NSLayoutAttributeTop,
        NSLayoutAttributeBottom,
        NSLayoutAttributeLeading,
        NSLayoutAttributeTrailing,
        NSLayoutAttributeWidth,
        NSLayoutAttributeHeight,
        NSLayoutAttributeCenterX,
        NSLayoutAttributeCenterY,
        NSLayoutAttributeBaseline,
    
        NSLayoutAttributeNotAnAttribute = 0
    };
    

    so you can check NSLayoutAttributeWidth for width.

    Sample code:

    NSArray constraints = [self constraints];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d", NSLayoutAttributeWidth];
    NSArray *filteredArray = [constraints filteredArrayUsingPredicate:predicate];
    if(filteredArray.count == 0){
          return nil;
    }
    NSLayoutConstraint *constraint =  [constraints objectAtIndex:0];
    

提交回复
热议问题