簡體   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