0

I would like to use FlowLayout to show two Items in one line by using the following code:

override func viewDidLoad()
    {
        super.viewDidLoad()
        setupCV()
    }

func setupCV()
    {
        collectionView.delegate = self
        collectionView.dataSource = self
        let width = (view.frame.size.width - 10) / 2
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: width, height: width + 80 )
    }

However, when I run the project, it shows me like that: enter image description here

When I debug it, the return value of the width is correct. enter image description here

The Size setting of the collection view is also correct I think: enter image description here

I also try to put the layout setting part in this function:

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

However, Xcode doesn't even excute this function.

Any Idea of what happened and how can I fix this issue? Many thanks!

LightOwlGG
  • 139
  • 10

1 Answers1

1

Set the layout back to collectionView:

let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: width, height: width + 80)
collectionView.collectionViewLayout = layout
Frankenstein
  • 15,732
  • 4
  • 22
  • 47
  • Thanks, it works. But why doesn't my code work? I have downcasted it to UICollectionViewFlowLayout. – LightOwlGG May 17 '20 at 21:46
  • 1
    You weren't changing the `itemSize` of the collectionView as I did. Instead you were changing `itemSize` of a variable. Which was never used. – Frankenstein May 18 '20 at 08:52