Byte
Byte

Reputation: 2940

Autolayout with dynamic specified spacing between views

I have been working with Autolayout for a while now and knew pretty much inside out of how it works. One thing I cannot figure out is the number in the visual representation.

For example, V:|-(20)-[someView] I want something more along the line of V:|-(x)-[someView].

I do not need the number to change after the the constraints are established. But given that I work with many constraints at the same time and I am trying to find out what is the best spacing for best UX. Instead of going through and replace each 20, I just want to just set x to the desired number and rebuild the app. (I know find/replace works but that is not robust and can easily create an unintended change if you are not careful)

The best I can come up with is using stringWithFormat but that increases the cluster in the code and not very scalable when it comes to complex views.

Upvotes: 0

Views: 158

Answers (1)

dariaa
dariaa

Reputation: 6385

I think metricsis exactly what you are looking for. Here is an example:

NSDictionary *views = @{@"blueView" : blueView};
NSDictionary *metrics = @{@"offset" : @(40)};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-offset-[blueView]-offset-|" options:0 metrics:metrics views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-offset-[blueView]-offset-|" options:0 metrics:metrics views:views]];

Upvotes: 3

Related Questions