'UILabel inside UIContentView jumps frames when delete accessory animates in

I am populating a UICollectionViewCell using the UIContentConfiguration + UIContentView APIs.

https://developer.apple.com/documentation/uikit/uicontentconfiguration

In my UIContentView subclass I have created a single label. When the cell's delete accessory slides in, the label's frame jumps choppily, rather than slide in smoothly. This only happens when the label's text alignment is set to anything besides natural or left. It looks very bad. Apple's own UIListContentConfiguration does not do this. Any ideas?

Video:

https://youtu.be/yXR9yZVFq04


let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, ItemIdentifier>.init { cell, indexPath, itemIdentifier in
    cell.contentConfiguration = ContentConfig(text: "Hello World")
    cell.accessories = [.delete(displayed: .whenEditing)]
}


struct ContentConfig: UIContentConfiguration, Equatable {
    
    func makeContentView() -> UIView & UIContentView {
        let contentView = ContentView()
        contentView.configuration = self
        return contentView
    }
    
    var text: String?
    
    func updated(for state: UIConfigurationState) -> ContentConfig {
        return self
    }
    
}

class ContentView: UIView, UIContentView {
    
    let label = UILabel()
    
    var configuration: UIContentConfiguration = ContentConfig() {
        didSet {
            let config = configuration as! ContentConfig
            label.text = config.text
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(label)
        label.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            label.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
            label.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
            label.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
            label.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
        ])
        label.textAlignment = .center
    }
    
    required init?(coder: NSCoder) {
        fatalError()
    }
    
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source