旋转设备

时间:2017-02-17 18:48:54

标签: ios swift containers uicollectionviewcell

晚上好。

首先我有一个SplitViewController。

在细节中,我有一个带有两个分段项的segmentController(ScreenOne,ScreenTwo)。

ScreenOne = TableViewController

ScreenTwo = CollectionViewController

我在细节中放了一个容器,这个容器可以是一个表或一个集合,具体取决于所选的段。

问题在于,当我将设备旋转为纵向并且当我旋转回到landscpae时,方法collectionView(_, layout: , sizeForItemAt:)无法进行正确的计算。

“我使用屏幕的宽度进行计算”

一开始我觉得问题是,当我得到视图的大小时,它不计算主人的大小。因为我使用容器和viewController分开,我想知道视图不知道splitView。

如果您有疑问,请与我联系。

我感谢任何帮助。

import UIKit

private let reuseIdentifier = "Cell"

class StudentsGroupCollectionViewController: UICollectionViewController {

    ///The sizes for the insets of the collection view
    fileprivate let sectionInsets = UIEdgeInsets(top: 30, left: 30, bottom: 30, right: 30)

    ///The amount of columns displayed per row in the collection view
    fileprivate let numColumns: Int = 3

    ///Reference to the size of the cells
    fileprivate var cellSize: CGSize!

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

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if let parent = parent {
            cellSize = collectionView?.calculateCellSizeWith(sectionInsets: sectionInsets, numColumns: numColumns, and: parent.view.frame.width)
        }

        print("The cellSize at the beggining: \(cellSize) - \(UIDevice.current.orientation.isPortrait ? "Por" : "Lan")")
    }

    override func viewDidAppear(_ animated: Bool) {

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 9
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)

        let label = cell.viewWithTag(1000) as! UILabel

        label.text = "\(indexPath.row + 1)"

        cell.backgroundColor = UIColor.blue

        return cell
    }

}



extension StudentsGroupCollectionViewController: 
UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        //TO DO: Cell size with different at rotation device

        return cellSize
    }

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

    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return sectionInsets.bottom
    }


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

        cellSize = collectionView?.calculateCellSizeWith(sectionInsets: sectionInsets, numColumns: numColumns, and: size.width)

        print("The cellSize at the transition: \(cellSize) - \(UIDevice.current.orientation.isPortrait ? "Por" : "Lan")")

        collectionView?.reloadData()
    }

}

我认为这里的主要方法是viewWillApper和最后一个viewWillTransiontion(对于size:)

我想要的方式 enter image description here下午好。

它的方式 enter image description here

0 个答案:

没有答案