I do a lot of work on apps where backward compatibility to really old iOS versions is a requirement so I'm only now starting to get up to speed with autolayout. I have been using autolayout for a while now and as long as you use IB and the view isn't too complex it's not too bad. However,
I now have a UIView subclass I'm loading from a nib I need to add to a view. The kicker is I need to add 1-3 instances on this view depending on. I realise I have to create constraints for this in my viewcontroller and attach to each custom view. These are height, width, center horizontally & top edge.
Previously doing the position calculation based on screen size and doing the resizing using strings and struts was only a couple of lines of code. So you could write a very tight loop to draw multiple instances into the screen.
I'm now facing this same problem using autolayout and although I have some working code I'm not happy with the sheer amount of it and was hoping for some help to make this better.
The below is just some test code. In reality the buttons array will hold 1-3 objects which will be used to seed each MyView instance. I want to tastefully put the MyView instances on the top half of the screen.
MyView is a UIView subclass with its own nib file. Init is overridden to return an instance loaded from the nib.
A couple of problems I would like to resolve with the above snippet.
1) Height and width of the view. I would like set this in IB in the nib of MyView, it doesn't feel natural to do this here.
2) In a loop like this, how can I use the previous view as reference to position the next?
3) I find I still have to calculate a starting y position and increment it. This is because problem (2).
4) Centring the view horizontally using visualFormat doesn't seem to work?
5) The above is a truck load of code. I'm lazy!
So what is a better way of achieving this.
I now have a UIView subclass I'm loading from a nib I need to add to a view. The kicker is I need to add 1-3 instances on this view depending on. I realise I have to create constraints for this in my viewcontroller and attach to each custom view. These are height, width, center horizontally & top edge.
Previously doing the position calculation based on screen size and doing the resizing using strings and struts was only a couple of lines of code. So you could write a very tight loop to draw multiple instances into the screen.
I'm now facing this same problem using autolayout and although I have some working code I'm not happy with the sheer amount of it and was hoping for some help to make this better.
The below is just some test code. In reality the buttons array will hold 1-3 objects which will be used to seed each MyView instance. I want to tastefully put the MyView instances on the top half of the screen.
MyView is a UIView subclass with its own nib file. Init is overridden to return an instance loaded from the nib.
Code:
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *buttons = @[@{@"title": @"view 1"}, @{@"title": @"view 2"}];
NSInteger y = ([[UIScreen mainScreen] bounds].size.height / 4.0) - (100 * buttons.count) / 2.0 + 30;
for (NSDictionary *btnDict in buttons) {
MyView *aView = [[MyView alloc] init];
[aView setButtonInfo:btnDict];
aView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:aView];
NSString *constraint = [NSString stringWithFormat:@"V:|-%li-[aView]", y];
y += 110;
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[aView(==300)]" // Set width to 300
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(aView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[aView(==100)]" // Set height to 100
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(aView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraint // Place one view under the other
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(aView)]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:aView // Center horizontally
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
}
}
A couple of problems I would like to resolve with the above snippet.
1) Height and width of the view. I would like set this in IB in the nib of MyView, it doesn't feel natural to do this here.
2) In a loop like this, how can I use the previous view as reference to position the next?
3) I find I still have to calculate a starting y position and increment it. This is because problem (2).
4) Centring the view horizontally using visualFormat doesn't seem to work?
5) The above is a truck load of code. I'm lazy!
So what is a better way of achieving this.