简体   繁体   中英

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]];

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:.

Luke Van In's answer on a fixed aspect ratio is a great answer!

Of course, if you simply want to use the same value in multiple places you would modify the code to use a metrics dictionary as follows:

 NSDictionary* metrics = @{@"stopButtonSize":@100};
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[stopButton(stopButtonSize)]" options:kNilOptions metrics:metrics views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[stopButton(stopButtonSize)]" options:kNilOptions metrics:metrics views:views]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM