gpt4 book ai didi

ios - constraintsWithVisualFormat 可以与动态生成的 View 一起使用吗?

转载 作者:行者123 更新时间:2023-11-29 12:23:14 24 4
gpt4 key购买 nike

我的模型中有一个标签列表。我想将它们作为标签添加到 tagView UIView 中作为 subview 。

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]]-|
^'

更新:

键似乎没问题。 enter image description here

最佳答案

您应该创建自己的字典,这样就可以添加没有您现在得到的额外括号的名称。像“list[0]”这样的名称似乎会使解析器感到困惑。

 NSMutableDictionary *views = [NSMutableDictionary new];
NSMutableArray *list = [NSMutableArray new];
for (long i=0; i<_tagView.subviews.count; i++) {
UILabel *tagLabel = _tagView.subviews[i];
[tagLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[list addObject:tagLabel];
[views setObject:tagLabel forKey:[NSString stringWithFormat:@"label%ld", i]];
}

NSDictionary *metrics = @{@"height":@25.0};

[_tagView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[label1]-[label2]-[label3]-|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:metrics views:views]];

关于ios - constraintsWithVisualFormat 可以与动态生成的 View 一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29978664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com