0
votes

I have a pager whole screen collectionview. When device rotation, first cell of collectionview is fitting to collectionview on landscape and portrait mode. But when I scroll next cell on collectionview, it is showing two cell. And it is not fitting all screen. How can I fit all cells size to collectionview although I scroll collectionview and rotate. I tried invalideLayout() and many way. But I couldn't achive. Just I only want, collectionviewcell fitted to whole screen in any situation although scroll and rotate.

P.S collectionviewcell size should be whole screen size.

public override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.current.orientation.isLandscape,
        let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
        let width = view.frame.height
        layout.itemSize = CGSize(width: width, height: UIScreen.main.bounds.width)
        layout.invalidateLayout()
    } else if UIDevice.current.orientation.isPortrait,
        let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
        let width = view.frame.width
        layout.itemSize = CGSize(width: width , height:  UIScreen.main.bounds.height)
        layout.invalidateLayout()
    }
}

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

    let width = view.frame.size.width
    return CGSize(width: width , height: UIScreen.main.bounds.height)
}
3
What have you written in sizeForItemAtIndexPath? - iPeter
Can you share the screenshots ? - Pooja Kamath
I added my question @iPeter - Burak Nurçiçek
Okay. Checking. - iPeter
What scrolling direction you want? Horizontal or vertical? - iPeter

3 Answers

0
votes

Make sure to remove spacing between your cells like below. This will resolve your problem.

enter image description here

0
votes

you have to write your code in viewWillLayoutSubviews(). Like this

override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews()

    guard let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
        return
    }



    if UIDevice.current.orientation == .landscapeLeft
    {
        flowLayout.itemSize = CGSize(width: self.view.frame.width, height: 500)
    }
    else if UIDevice.current.orientation == .landscapeRight
    {
        flowLayout.itemSize = CGSize(width: self.view.frame.width, height: 500)
    }

    flowLayout.invalidateLayout()
}
0
votes

can you try to Apply following Approach.

1'st Approach

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

   super.viewWillTransition(to: size, with: coordinator)

   // YOUR CODE OR FUNCTIONS CALL HERE

}

2' nd Approach

override func viewDidLoad() {
    super.viewDidLoad()        

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}

deinit {
     NotificationCenter.default.removeObserver(self)
}

func rotated() {
    if UIDevice.current.orientation.isLandscape,
        let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
        let width = view.frame.height
        layout.itemSize = CGSize(width: width, height: UIScreen.main.bounds.width)
        layout.invalidateLayout()
    } else if UIDevice.current.orientation.isPortrait,
        let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
        let width = view.frame.width
        layout.itemSize = CGSize(width: width , height:  UIScreen.main.bounds.height)
        layout.invalidateLayout()
    }
}