我有我的模型中的标签列表。我想将它们作为标签添加到tagView
UIView中作为子视图。constraintsWithVisualFormat可以与动态生成的视图一起使用吗?
NSMutableArray *list = [NSMutableArray new];
for (long i=0; i<_tagView.subviews.count; i++) {
UILabel *tagLabel = _tagView.subviews[i];
[tagLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[list addObject:tagLabel];
}
// Create the views and metrics dictionaries
NSDictionary *metrics = @{@"height":@25.0};
NSDictionary *views = NSDictionaryOfVariableBindings(list[0], list[1], list[2]);
// This is experimental as I assume there are three subviews in that.
[_tagView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[list[0]]-[list[1]]-[list[2]]-|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:metrics views:views]];
这是可行的还是必须采取不同的方向?
我忘了补充错误消息我得到:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format:
list is not a key in the views dictionary.
|-[list[0]]-[list[1]]-[list[2]]-|
^'
UPDATE:
键似乎是罚款。
好主意。我刚刚添加了屏幕截图。似乎没关系,不是吗?但仍然没有工作。 – Houman
对不起,我删除了我的评论,因为我对它进行了测试,并且按照您的要求显示。但是,我认为这个关键是由于额外的括号导致了错误。不要使用NSDictionaryOfVariableBindings,而是将密钥添加到可循环字典中,如label1,label2等。 – rdelmar