简体   繁体   English

来自代码的NSLayoutConstraints

[英]NSLayoutConstraints from code

I have the next problem: I have an undefined number of UILabels, there might be 2 labels or 10 labels, and i add them at runtime. 我有下一个问题:我的UILabel数量不确定,可能有2个标签或10个标签,然后在运行时添加它们。 and i want those labels to appear in the view in two columns. 我希望这些标签出现在视图的两列中。 I want to do this with NSLayoutConstraints. 我想用NSLayoutConstraints做到这一点。 if i have just 2 labels i managed to do it, but if there are more it just doesn't work. 如果我只有2个标签,我设法做到了,但是如果还有更多标签,那是行不通的。 I add the labels in a for loop. 我将标签添加到for循环中。 Can anybody help me? 有谁能够帮助我? And one more question: if i have this code: 还有一个问题:如果我有这段代码:

[myView addConstraints:[NSLayoutConstraint 
constraintsWithVisualFormat:@"H:|-0-[label1(label2)]-[separator(1)]-[label2]-0-|"
                                                                   options:0
                                                                   metrics:nil
                                                                     views:views]];

what does [label1(label2)] mean? [label1(label2)]是什么意思? Thank you 谢谢

H:|-0-[label1(label2)]-[separator(1)]-[label2]-0-| H:| -0- [label1(label2)]-[分隔符(1)]-[label2] -0- ||

1.H:means horizontal 1.H:表示水平

2.| 2. | means the edge of parent View 表示父视图的边缘

3.[] means it's have a subview inside, in this case , subview is label1. 3. []表示内部有一个子视图,在这种情况下,子视图为label1。

4.- means connect 4.-表示连接

5.-number- means padding number point 5.-数字-表示填充数字点

6.all the subview like (label1, separator, label2) must be the key of views(last params) 6,所有子视图(如label1,separator,label2)必须是视图的关键(最后一个参数)

7.() means the width/height of the subview.like [subview(100)] means subview's width = 100 7.()表示子视图的宽度/高度。例如[subview(100)]表示子视图的宽度= 100

see 看到

[document] https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage/VisualFormatLanguage.html#//apple_ref/doc/uid/TP40010853-CH3 [document] https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage/VisualFormatLanguage.html#//apple_ref/doc/uid/TP40010853-CH3

what does [label1(label2)] mean? [label1(label2)]是什么意思? Thank you 谢谢

it's mean label1 equal label2 (if prefix H - width, if prefix V - height) 等于label1等于label2(如果前缀H-宽度,如果前缀V-高度)

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

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