iam*_*old 3 objective-c uiview ios nslayoutconstraint
我有一个包含多个UIView子视图的UIView,它们有自己的约束.如何删除子视图的约束?
//only removes the constraints on self.view
[self.view removeConstraints:self.view.constraints];
//I get warning: Incompatible pointer types sending 'NSArray *' to parameter of type 'NSLayoutConstraint'
[self.subview1 removeConstraints:self.subview1.constraints];
Run Code Online (Sandbox Code Playgroud)
试试这段代码:
for (NSLayoutConstraint *constraint in self.view.constraints) {
if (constraint.firstItem == self.subview1 || constraint.secondItem == self.subview1) {
[self.view removeConstraint:constraint];
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,这会迭代分配给的所有约束,self.view并检查是否self.subview1涉及约束.如果是这样,那个约束就被拉了.
您必须删除视图及其子视图的所有约束。因此,创建 UIView 的扩展,然后定义以下方法:
extension UIView {
func removeAllConstraints() {
self.removeConstraints(self.constraints)
for view in self.subviews {
view.removeAllConstraints()
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后调用以下命令:
self.view.removeAllConstraints()
Run Code Online (Sandbox Code Playgroud)
正如后来回答的那样,这是很快的。这可能对你有帮助。
| 归档时间: |
|
| 查看次数: |
5626 次 |
| 最近记录: |