更改CollectionViewCell设备方向更改

时间:2020-05-05 04:00:55

标签: ios swift xcode uicollectionview

我真的需要帮助。我正在设计一个记录笔记的应用程序,其中的CollectionViewController页面包含笔记本。当从风景肖像中翻转视图时,我正在努力调整笔记本的大小。我也只希望每行三个笔记本。我附上一张图片,以便大家明白我的意思。

问题是,当您纵向构建笔记本并将其翻转为横向时,单行最多可以容纳6个笔记本。当您横向放置笔记本并将其纵向放置时,连续最多只能容纳2个笔记本。此外,翻转方向时偶尔会丢掉笔记本的尺寸。

这一切与更改设备方向时正确调整集合视图单元格大小的能力有关。我将从我的CollectionViewController附加所有代码,并将尽我所能解释它。

var arrayOfNotebooks: [Notebook] = []
var setOfNotebookNames = Set<String>()

class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

@IBOutlet var CollectionViewOutlet: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()

    //Sets the side inserts of the CollectionView
    let flow = CollectionViewOutlet.collectionViewLayout as! UICollectionViewFlowLayout
       flow.sectionInset = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50)
       flow.minimumLineSpacing = 50

    //Creates a new Unfiled Notes notebook if needed
    if (arrayOfNotebooks.count == 0) {
        let newNotebook = Notebook(notebookName: "Unfiled Notes", date: Date())
        arrayOfNotebooks.insert(newNotebook, at: 0)
        setOfNotebookNames.insert("Unfiled Notes")
    }
    //Updates the UI
    NotificationCenter.default.addObserver(forName: .updateInterface, object: nil, queue: OperationQueue.main) { (notification) in
        self.updateUI()
    }

}

//Creates the CodeNotes header at the top of the collectionView
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    if (kind == UICollectionView.elementKindSectionHeader) {
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath)
        // Customize headerView here
        return headerView
    }
    fatalError()
}

//Sets the size of the cells
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: ((CollectionViewOutlet.frame.size.width) / 4), height: ((CollectionViewOutlet.frame.size.height) / 4))
}


//Returns number of notebooks to put into the collectionView
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return arrayOfNotebooks.count
}

//Returns the cell object built on the main storyboard
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! NotebookCell

    //Update label with name and date for every notebook in the array
    cell.notebookLabel.text = arrayOfNotebooks[indexPath.row].notebookName
    cell.notebookLabel.font = cell.notebookLabel.font.withSize(self.view.frame.width * 0.022)
    cell.dateLabel.text = cell.convertDate(date: arrayOfNotebooks[indexPath.row].date)
    cell.dateLabel.font = cell.dateLabel.font.withSize(self.view.frame.width * 0.05)
    //((pow(self.view.frame.width, 2) + pow(self.view.frame.height, 2)).squareRoot() * 0.014)
    return cell
}

//Updates the UI of the homepage
func updateUI() {
    CollectionViewOutlet.reloadData()
    print(setOfNotebookNames)
}

这是我用来构建collectionViewController的代码。笔记本数组是我要添加到集合视图中的笔记本对象,而集合有助于我确保没有重复的名称。

我再次只希望三个笔记本能够在纵向或横向模式下正确调整大小。我很乐于提供帮助,因为我在网上阅读了很多书,但是经过几天的努力,我仍然在努力。谢谢。

当我尝试在横向上构建笔记本时,会发生这种情况,所有尺寸都弄得一团糟:

然后,当我将其放回肖像中时,一行只能得到2个笔记本:

这是它的外观:

0 个答案:

没有答案