簡體   English   中英

動態更改autolayout控制的視圖的寬高比

[英]Dynamically change aspect ratio of view controlled by autolayout

問題

在UI中,我有一些視圖,哪些內容是可伸縮的,但應該保留一些寬高比。 此寬高比在運行時更改,並且在描述寬高比的某些屬性更改時應更新視圖布局。

我已經公開了NSLayoutConstraint ,但其屬性用於寬高比: multiplier是只讀的。 什么是更有趣的類似屬性constant可以改變。

可能的解決方案

  1. 我可以使用固定寬度或高度的約束並根據預期比率計算它,但這需要額外的鍋爐板代碼,這將對幾何/布局變化作出反應,我寧願避免這種情況。
  2. 我可以嘗試使用intrisicContentSizesizeThatFits:但我不確定這應該如何與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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM