繁体   English   中英

在代码中查找在IB中创建的布局约束

[英]Find in code a layout constraint created in IB

我想用代码制作在IB中创建的约束的动画。 我想找到底部LayoutGuide和UIView底部边缘之间的约束。

约束实例非常不透明:firstItem和secondItem是AnyObject,因此有很多强制转换。 除了在_stdlib_getTypeName()上进行字符串比较外,很难看到我将如何决定哪些约束涉及LayoutGuides。

还是应该删除所有约束并即时重新创建它们? (但是,IB的意义何在?由于我的情节提要板使用自动布局,因此无论如何我都必须在IB中添加约束。)

在Interface Builder中单击约束,并为其创建IBOutlet ,就像对按钮,文本视图等一样。

您还可以在运行时使用类似以下的内容找到它:

NSLayoutConstraint *desiredConstraint;
for (NSLayoutConstraint *constraint in putYourViewHere.constraints) {
    if (constraint.firstAttribute == NSLayoutAttributeHeight) { // Or whatever attribute you're looking for - you can do more tests
        desiredConstraint = constraint;
        break;
    }
}

这应该很简单。 如前所述,为要设置动画的约束创建一个IBOutlet:

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *verticalSpaceLayoutConstraint;

然后,当您要对其进行动画处理时(另请参见: 如何为约束更改设置动画? ),如果需要在具有约束的视图中强制进行布局传递,将约束更新为所需的值,执行UIView动画并强制布局传递为动画持续时间所需:

- (void)moveViewSomewhere { 
    [self.view layoutIfNeeded];

    _verticalSpaceLayoutConstraint.constant = 10;  // The value you want your constraint to have when the animation completes.
    [UIView animateWithDuration:1.0
        animations:^{
            [self.view layoutIfNeeded];
        }];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM