resize superview after subviews change dynamically using autolayout

后端 未结 4 1660
难免孤独
难免孤独 2020-12-04 05:53

I cant for the love of god the the hang of this resizing superview.

I have a UIView *superview with 4 UILabels. 2 function as header for t

4条回答
  •  生来不讨喜
    2020-12-04 06:16

    This was made dramatically easier with the introduction of Stack Views in iOS 9. Use a stack view inside your view to contain all your content that resizes, and then simply call

    view.setNeedsUpdateConstraints()
    view.updateConstraintsIfNeeded()
    view.setNeedsLayout()
    view.layoutIfNeeded()
    

    after changing your content. Then you can get your new size by calling

    view.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
    

    if you ever need to calculate the exact size required for a view.

提交回复
热议问题