Zenman C
Zenman C

Reputation: 1723

How to fix item width in UIcollectionView when moving from portrait to landscape?

I'm new to Swift and am trying to implement an app that has a 'presentation' mode. I'm trying to carry this out by using a UICollectionView where the cell in both portrait and landscape mode is the full width of the frame that it's in and half its height. The problem is that when I start the app in landscape or portrait mode everything works fine. However, if I start in portrait mode and then rotate 'right' to landscape mode, the cell no longer fits the width of the frame.

What I've tried so far is:

None of which have worked.

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

let textArray = ["Some text", "Some other text", "even more stuff", "And more stuff", "And this is more text"]

@IBOutlet weak var collectionView: UICollectionView!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    collectionView.dataSource = self
    collectionView.delegate = self

    setUpCollectionView()
}

// setUpCollectionView()

func setUpCollectionView() {

    if let flowLayOut = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
        flowLayOut.scrollDirection = .horizontal
        flowLayOut.minimumLineSpacing = 0
    }
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return textArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! TextCell

    cell.textView.text = textArray[indexPath.row]

    collectionView.isPagingEnabled = true

    return cell
}

// MARK: - Make the cell fill the view size

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

    return CGSize(width: view.frame.width, height: view.frame.height / 2)
}

}

Be grateful for any help. What I'd like is for the cell to be consistently the full width of the frame whether in landscape or in portrait.

Screenshot in portrait(correct) !(https://photos.app.goo.gl/nozLPGF8X7E1aNDE6)

Screenshot in landscape(not correct) !https://photos.app.goo.gl/YCTJdmqVY6eKkpYS8

Screenshot in landscape(correct) https://photos.app.goo.gl/pjWyC1oZeckUuHE66

Upvotes: 2

Views: 1036

Answers (1)

Kamran
Kamran

Reputation: 15248

You can override viewWillTransition method and reload so that collectionView recalculate the size.

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)

    self.collectionView?.reloadData()
}

Upvotes: 0

Related Questions