2

I need change center of some views programmatically. 10 or more. I don't want to adding each bottom constraint as outlet, so could I search them from code like as:

for(NSLayoutConstraint *constraint in view.constraints)

Which attributes should I check? I need constraint for bottom space of superview.

Viktorianec
  • 361
  • 6
  • 22
  • 1
    You can create an OutletCollection with all the constraints and iterate over it like a normal array. – Swapnil Luktuke Mar 30 '15 at 14:23
  • But I have buttons, labels etc. Should OutletCollection work with different types of views? – Viktorianec Mar 30 '15 at 14:26
  • 1
    You need to connect the 'constraint' not the view. Just like all the views you can create an IBOutlet or an IBOutletCollection for layout constraints. It does not matter which view the constraint belongs to... – Swapnil Luktuke Mar 30 '15 at 14:28

1 Answers1

3

Have you heard about IBOutletCollection? When you link any object from IB to VC code, in popup window you can select IBOutletCollection Option. After link all constraints you want, only iterate this collection :)

@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *contentViewConstraints;


for (int i = 0; i < contentViewConstraints.count; i++) {
    NSLayoutConstraint *constraint = contentViewConstraints[i];
    //Center here
}
Klevison
  • 3,342
  • 2
  • 19
  • 32