[英]Dynamically change aspect ratio of view controlled by autolayout
在UI中,我有一些視圖,哪些內容是可伸縮的,但應該保留一些寬高比。 此寬高比在運行時更改,並且在描述寬高比的某些屬性更改時應更新視圖布局。
我已經公開了NSLayoutConstraint
,但其屬性用於寬高比: multiplier
是只讀的。 什么是更有趣的類似屬性constant
可以改變。
intrisicContentSize
和sizeThatFits:
但我不確定這應該如何與NSLayoutConstraint
一起使用 好吧,我抱怨很多。 刪除和重新創建約束不是一個大問題(感謝@vacawama):
- (void)updateASpectRatio: (float)aspectRatio {
// self.aspectRatioConstraint - has to be a strong reference
[self.aspectRatioView removeConstraint: self.aspectRatioConstraint];
self.aspectRatioConstraint = [NSLayoutConstraint constraintWithItem: self.aspectRatioConstraint.firstItem
attribute: self.aspectRatioConstraint.firstAttribute
relatedBy: self.aspectRatioConstraint.relation
toItem: self.aspectRatioConstraint.secondItem
attribute: self.aspectRatioConstraint.secondAttribute
multiplier: self.aspectRatioView.aspectRatio
constant: 0.0];
[self.aspectRatioView addConstraint: self.aspectRatioConstraint];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.