2

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:

  • moving my setUpCollectionView() function in and out of my viewDidLoad
  • adjusting my sizeForItem at function
  • adjusting autolayout constraints

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

Zenman C
  • 1,653
  • 13
  • 21
  • Possible duplicate of [UICollectionView - resizing cells on device rotate - Swift](https://stackoverflow.com/questions/34132766/uicollectionview-resizing-cells-on-device-rotate-swift) – zero3nna Apr 03 '19 at 07:31

1 Answers1

0

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()
}
Kamran
  • 14,987
  • 4
  • 33
  • 51