UISlider as UICollectionViewListCell accessory running off edge of cell

291 Views Asked by At

Does anyone know why my UISlider runs off the edge of my list cell? I am configuring it as a custom accessory.

Code:

    let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, String>.init { cell, indexPath, itemIdentifier in
        var config = cell.defaultContentConfiguration()
        config.text = itemIdentifier
        cell.contentConfiguration = config
        let sliderAccessory = UICellAccessory.customView(configuration: .init(customView: UISlider(), placement: .trailing()))
        cell.accessories = [sliderAccessory]
    }

Result:

enter image description here

1

There are 1 best solutions below

0
MH175 On

The answer is you have to set maintainsFixedSize to true and reservedLayoutWidth to actual. Define the width of the slider in the slider's init(frame:) initializer.

    let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, String>.init { cell, indexPath, itemIdentifier in
        var config = ...
        let slider = UISlider(frame: CGRect(x: 0, y: 0, width: 100, height: 80))
        let sliderAccessory = UICellAccessory.customView(configuration: .init(customView: slider, placement: .trailing(), reservedLayoutWidth: .actual, maintainsFixedSize: true))
        cell.accessories = [sliderAccessory]
    }