WishIHadThreeGuns
WishIHadThreeGuns

Reputation: 1479

Rotating a UICollectionView leads to error

When I rotate the device with a UICollectionView I get an error:

the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.

I think that this mistake is within my UICollectionViewDelegateFlowLayout where I try to set the height

    if UIApplication.shared.statusBarOrientation.isLandscape {
        height = min( Constants.screenWidth, Constants.screenHeight )
    }

    return CGSize(width: view.bounds.width, height: (height - 300) / 1 )

as you can see I've tried to alter the height for the orientation, but ultimately this does not change as I thought it should.

Complete code is in this GitHub link: https://github.com/stevencurtis/CollectionViewRotate

I want to stop getting this error, and ultimately because the height is incorrectly set when I rotate the device the image is larger than the bounds of the view.

How can I set up my flow layout so this doesn't happen.

Upvotes: 1

Views: 75

Answers (1)

vacawama
vacawama

Reputation: 154711

The height of the screen is irrelevant. What you really care about is the height of the collectionView.

Use:

let height = collectionView.bounds.height

By using that value, the height will be correct when you rotate the phone without your having to check for it explicitly. You'll still want to adjust for insets if you have any.

You should also use collectionView.bounds.width for the width in place of view.bounds.width.

Upvotes: 1

Related Questions