【发布时间】:2018-04-24 14:50:12
【问题描述】:
我有多个文本字段,我想对其应用类似的布局约束。我没有重复这些约束,而是创建了一个 (UITextField, UIView) 类型的元组数组,其中 UITextField 是我要添加约束的文本字段,视图是包含此类文本字段的视图。
我的代码如下:
let textFields: [(UITextField, UIView)] = ...
for textFieldTuple in textFields {
let textField = textFieldTuple.0!
let view = textFieldTuple.1!
textField.snp.makeConstraints { (make) -> Void in
make.top.equalTo(view.snp.top)
make.bottom.equalTo(view.snp.bottom)
make.left.equalTo(view.snp.left)
make.right.equalTo(view.snp.right)
}
}
但是,当我运行我的应用程序时,由于闭包中的第一个约束,应用程序崩溃了。我是否能够从这样的位置进行约束,或者我应该为每个 TextField 显式编写它们?谢谢
崩溃详情:
libc++abi.dylib: terminating with uncaught exception of type NSException
【问题讨论】:
-
发布崩溃 ....
-
在设置约束之前是否将每个文本字段添加到它的视图中??
-
不,我在添加约束后添加它们
-
那就是原因
-
解决了,谢谢!