Jean Valjean
Jean Valjean

Reputation: 736

IOS 11: UICollectionView on iPhone X is sized wrong

I have a collection view that the frame appears too tall on the iPhone X. On every other device, the sizing and scrolling works properly as shown below:

enter image description here

However on iPhone X, it looks like this:

enter image description here

The top row is cut off, and it does not scroll all the way down to the last row. Somehow, the sizing correctly calculates the width but not the height, which is about 70 pixels too tall. (I'm not worried about the top and bottom bars. I'll fix those later)

I'm guessing this has something to do with the inset adjustments for the iPhone X screen, but I can't figure out how to fix it. I've tried this in where I size the collection view:

if #available(iOS 11.0, *) {
    collectionView.contentInsetAdjustmentBehavior = .always
}

However, I can't seem to fix it.

Edit:

To clarify, the set up for this menu is as follows, there are actually two collection views onscreen. The first scrolls horizontally with paging enabled so that it locks onto each cell. The other collection views are the cells for the first one, and they scroll vertically. We'll call these subCollectionViews.

The subCollectionViews are receiving a size from the original collection view thats too tall. On the storyboard, the collection view's height is defined with respect to the top bar and the bottom paging bar as flush. In the story board, the height of the collection view is about 70 pixels larger than the calculated height during runtime.

So for the cell's layout guide:

override func awakeFromNib() {
    cellImage = UIImageView(frame: contentView.frame)
    cellImage?.contentMode = .scaleAspectFill
    cellImage?.clipsToBounds = true

    contentView.addSubview(cellImage!)
}

and for the collection view's layout:

        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 8
        layout.minimumInteritemSpacing = 8

for the storyboard presets:

enter image description here

I think this is what y'all asked for. If there's anything you need to see, just ask.

Upvotes: 2

Views: 3477

Answers (2)

Nizzam
Nizzam

Reputation: 1050

I also facing this issue. Need to called safeAreaLayoutGuide for control the size of item.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    if #available(iOS 11.0, *) {
        return view.safeAreaLayoutGuide.layoutFrame.size
    } else {
        // Fallback on earlier versions
        return view.frame.size
    }
}

Upvotes: 11

Renish Dadhaniya
Renish Dadhaniya

Reputation: 10752

Add below lines of code. I think it will solve your problem.

        override func viewWillAppear(_ animated: Bool) {
                super.viewWillAppear(animated)

                self.aCollectionVew.setNeedsLayout()
                self.aCollectionVew.layoutIfNeeded()
         }

Please take all traits, spacing and contentInsetAdjustmentBehavior.

Happy Coding...:)

Upvotes: 3

Related Questions