D-A UK
D-A UK

Reputation: 1174

Error when trying to convert NSLayoutDimension to CGFloat

I am currently trying to convert an NSLayoutDimension to a CGFloat, so that I can get my collection view to conform to a certain layout.

    let spacing: CGFloat = 3

    let width = CGFloat(myCollectionView.widthAnchor)

    let height = CGFloat(myCollectionView.heightAnchor)


    let itemWidth = width / 3 - spacing
    let itemHeight = height / 3 - spacing

    var itemSize: CGFloat = 0

    if itemWidth >= itemHeight {
        itemSize = itemWidth
    }
    else if itemHeight > itemWidth {
        itemSize = itemHeight
    }

    let layout = UICollectionViewFlowLayout()

    layout.sectionInset = UIEdgeInsetsMake(20, 0, 10, 0)
    layout.itemSize = CGSize(width: itemWidth, height: itemHeight)

    layout.minimumInteritemSpacing = spacing
    layout.minimumLineSpacing = spacing

    myCollectionView.collectionViewLayout = layout

Setting width and height is giving the error of:

Cannot invoke initializer for type 'CGFloat' with an argument list of type '(NSLayoutDimension)'.

I have tried setting as a float and then converting float to cgfloat but that has not worked either.

Does anyone know how to convert NSLayoutDimension to CGFloat?

Any help is much appreciated

Thanks.

Upvotes: 1

Views: 4498

Answers (1)

Shezad
Shezad

Reputation: 756

if you want width and height of your collection view try the code below:

let width = CGFloat(myCollectionView.frame.width)

let height = CGFloat(myCollectionView.frame.height)

Upvotes: 5

Related Questions