-systemLayoutSizeFittingSize: returning incorrect height for tableHeaderView under iOS 8

前端 未结 6 1806
旧时难觅i
旧时难觅i 2021-02-07 00:08

There are numerous threads about correctly sizing a tableHeaderView with auto-layout (one such thread) but they tend to pre-date iOS 8.

I have a situation with numerous

6条回答
  •  抹茶落季
    2021-02-07 00:55

    The proper solution for this problem in Swift 3 is using this class instead of standard UILabel:

    class UILabelPreferedWidth : UILabel {
        override var bounds: CGRect {
            didSet {
                if (bounds.size.width != oldValue.size.width) {
                    self.setNeedsUpdateConstraints()
                }
            }
        }
    
        override func updateConstraints() {
            if(preferredMaxLayoutWidth != bounds.size.width) {
                preferredMaxLayoutWidth = bounds.size.width
            }
            super.updateConstraints()
        }
    }
    

    Also make sure that you didn't disable those two on your Cell class:

    translatesAutoresizingMaskIntoConstraints = false
    contentView.translatesAutoresizingMaskIntoConstraints = false
    

提交回复
热议问题