简体   繁体   中英

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. and i want those labels to appear in the view in two columns. I want to do this with NSLayoutConstraints. if i have just 2 labels i managed to do it, but if there are more it just doesn't work. I add the labels in a for loop. 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? Thank you

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

1.H:means horizontal

2.| means the edge of parent View

3.[] means it's have a subview inside, in this case , subview is label1.

4.- means connect

5.-number- means padding number point

6.all the subview like (label1, separator, label2) must be the key of views(last params)

7.() means the width/height of the subview.like [subview(100)] means subview's width = 100

see

[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? Thank you

it's mean label1 equal label2 (if prefix H - width, if prefix V - height)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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