Search code examples
iosobjective-cautolayoutvisual-format-language

Using the autolayout visual format language is it possible to set the width of a view equal to the height of the same view?


Using the autolayout visual format language is it possible to set the width of a view equal to the height of the same view?

Here is what I want to do:

[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[stopButton(100)]" options:kNilOptions metrics:nil views:views]];
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[stopButton(stopButton.height)" options:kNilOptions metrics:nil views:views]];

Solution

  • You would need to create a fixed aspect ratio constraint which, according to the documentation for the visual formatting language, is not possible currently.

    One useful constraint that cannot be expressed is a fixed aspect ratio (for example, imageView.width = 2 * imageView.height). To create such a constraint, you must use constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:.