繁体   English   中英

使用autolayout可视化格式语言可以将视图的宽度设置为等于同一视图的高度吗?

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

使用autolayout可视化格式语言可以将视图的宽度设置为等于同一视图的高度吗?

这是我想要做的:

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

您需要创建一个固定的宽高比约束,根据可视化格式语言文档,该约束目前是不可能的。

一个无法表达的有用约束是固定的宽高比(例如,imageView.width = 2 * imageView.height)。 要创建这样的约束,必须使用constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:。

Luke Van In对固定宽高比的回答是一个很好的答案!

当然,如果您只想在多个位置使用相同的值,则可以修改代码以使用度量字典,如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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