簡體   English   中英

Swift - 未按時調用 intrinsicContentSize

[英]Swift - intrinsicContentSize is not called on time

我的界面構建器中有以下結構:

MyCustomView
   StackView 
      Label
      TextField
      Label - is hidden
Button

當按下按鈕時,CustomView 的邏輯應該使底部的 label 出現,因此再次計算 IntrinsicContentSize。 不幸的是,只有在第二個按鈕單擊后才能正確顯示視圖。

以下是相關代碼:

public class MyCustomView: UIView {

...

var subtitle: String! {
    didSet {
        subtitleLabel.isHidden = subtitle.isEmpty
        subtitleLabel.text = subtitle
        invalidateIntrinsicContentSize()
    }
}

....

override public var intrinsicContentSize: CGSize {
    stackView.layoutIfNeeded()
    return stackView.bounds.size
}

....

}

在此處輸入圖像描述 在此處輸入圖像描述

在取消隱藏 stackView 子視圖之一時,我缺少的行是stackView.setNeedsLayout()

所以這是工作didSet

var subtitle: String! {
    didSet {
        subtitleLabel.isHidden = subtitle.isEmpty
        subtitleLabel.text = subtitle
        stackView.setNeedsLayout()
        invalidateIntrinsicContentSize()
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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