I'm trying to figure out autolayout recently. Almost I thought that I understand it with editor but when it comes to manage them with programmatically it's really hard.
Anyway let me explain the problem which i try to solve;
I have a tableviewcell with autolayout. In this cell I have a subview which i want to fill it later with code. So far it's fine. I also a custom view controller designed with autolayout. What I want to accomplish is repeating n times these different instance of view controllers view inside of subview with margin of lets say 10px to previous item.
Here is the hierarchy of the views;
--cell
----label1
----label2
----subview
------view controller's view 1
------view controller's view 2
------view controller's view 3
..
..
------view controller's view n
----button
----button
Here is the code I came with so far;
self
refers to tableviewcell
self.workoutView
refers to subview
SPPostWorkoutItemViewController
refers to inner controllers
.
SPPostWorkoutItemViewController *previousController = nil;
for(int i=0; i < p.workout.workoutItems.count; i++ ){
WorkoutItem *workoutItem = p.workout.workoutItems[i];
SPPostWorkoutItemViewController *workoutItemController = [[SPPostWorkoutItemViewController alloc]initWithWorkoutItem:workoutItem];
if(previousController == nil){
NSLayoutConstraint * cons = [NSLayoutConstraint constraintWithItem:workoutItemController.view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.workoutView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:10];
[self.workoutView addConstraint:cons];
}
else{
NSLayoutConstraint * cons = [NSLayoutConstraint constraintWithItem:workoutItemController.view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:previousController.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:10];
[self.workoutView addConstraint:cons];
}
[self.workoutView addSubview:workoutItemController.view];
previousController = workoutItemController;
}