简体   繁体   中英

Can't get simple self-sizing collection view cell to size to label using autolayout

Xcode 10.2, Swift 5.

I've got a UICollectionViewCell defined in a .xib file. I load that in my UIViewController (not UICollectionViewController ) subclass and return it in cellForItemAt .

In my UICollectionViewCell (called TagCell ), I do this:

    override
    func
    awakeFromNib()
    {
        super.awakeFromNib()

        self.contentView.translatesAutoresizingMaskIntoConstraints = false
        self.translatesAutoresizingMaskIntoConstraints = false

        self.layer.borderColor = UIColor(red: 0.354, green: 0.442, blue: 0.297, alpha: 0.74).cgColor
        self.layer.borderWidth = 1.0
        self.layer.cornerRadius = 4.0
    }

The Nib has only the top-level UICollectionViewCell that I dropped in, and a UILabel with fixed 4-pixel constraints to all four edges. I set the text of the UILabel in cellForItemAt . However, the resulting collection is rendered with all cells at the estimated height I set in viewDidLoad() :

    override
    func
    viewDidLoad()
    {
        super.viewDidLoad()

        let nib = UINib(nibName: "TagCell", bundle: Bundle(for: type(of: self)))
        self.collectionView.register(nib, forCellWithReuseIdentifier: "TagCell")

        let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.estimatedItemSize = CGSize(width: 60.0, height: 25.0)
}

Cell Nib File:

Cell Nib截图

This seems to be anything Apple and other resources I found online tell me to do, but I must be missing something crucial. I've also tried setting the horiztontal compression resistance priority to 1000 on my UILabel, but they keep getting truncated.

I aso tried putting in a >= width constraint, but that appears to be ignored.

Before I set self.contentView.translatesAutoresizingMaskIntoConstraints , I would get the conflicting constraints warning in the debugger. But now it doesn't complain about the >= width constraint, even thought it's violating it.

So, it turns out you must do the following in your cell's awakeFromNib() :

        self.contentView.translatesAutoresizingMaskIntoConstraints = false
        let fixupConsts =
        [
            self.contentView.leftAnchor.constraint(equalTo: self.leftAnchor),
            self.contentView.rightAnchor.constraint(equalTo: self.rightAnchor),
            self.contentView.topAnchor.constraint(equalTo: self.topAnchor),
            self.contentView.bottomAnchor.constraint(equalTo: self.bottomAnchor)
        ]
        NSLayoutConstraint.activate(fixupConsts)

Despite the fact that WWDC 2014 Session 226 very explicitly states the only thing you need to do is set estimatedCellSize to a non-zero value, it's simply not enough.

If anyone can provide a better answer, I'll gladly mark it as the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM