CollectionView Issue on iPhone 7/8 plus, iPhone 7/

2019-08-06 23:16发布

问题:

This is probably a simple issue but, I don't know how to get rid of it.

My problem:

  • When I run on iPhone 8 Plus, my CollectionView is exactly what I expect (see Image1).
  • When I run on iPhone X, my CollectionView has a different layout (see Image2).

What I would like:

Using a UICollectionView, I would like to get a layout like Image1, on every single iPhone.

Could you pls give me some tips to solve this issue ?

Image1 (Expected Result):

Result on the iPhone 8 Plus

Image2 (Wrong Result):

Result on the iPhone X

回答1:

Use following Delegate method: UICollectionViewDelegateFlowLayout

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

          return CGSize(width: (self.view.frame.width - 8) / 3.0 , height: (self.view.frame.width - 8) / 3.0)
     }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

        return UIEdgeInsetsMake(8, 8, 0, 8)
    }