使用自动布局视觉格式语言是否可以将视图的宽度设置为等于同一视图的高度?
Using the autolayout visual format language is it possible to set the width of a view equal to the height of the same view?
使用自动布局视觉格式语言是否可以将视图的宽度设置为等于同一视图的高度?
这是我想要做的:
[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]];
您需要创建一个固定的纵横比约束,根据 documentation for the visual formatting language,目前无法实现。
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 关于固定纵横比的回答非常好!
当然,如果您只是想在多个地方使用相同的值,您可以修改代码以使用度量字典,如下所示:
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]];
使用自动布局视觉格式语言是否可以将视图的宽度设置为等于同一视图的高度?
这是我想要做的:
[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]];
您需要创建一个固定的纵横比约束,根据 documentation for the visual formatting language,目前无法实现。
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 关于固定纵横比的回答非常好!
当然,如果您只是想在多个地方使用相同的值,您可以修改代码以使用度量字典,如下所示:
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]];