当设备从纵向模式进入横向模式时,水平集合视图单元内的垂直集合视图单元格不会调整大小

时间:2018-06-26 06:01:42

标签: ios

我一直在尝试在水平集合视图内实现垂直集合视图。我最大程度地成功了自己的事业。但是,当设备从纵向模式旋转到横向模式时,垂直集合视图单元格不会调整大小。尽管当设备从横向模式进入纵向模式时,单元格的大小可以正确调整。由于我使viewWillTransitionToSize函数内部的流布局无效,因此即使水平集合视图单元格也可以正确调整大小。我也为垂直集合视图(在单独的CocoaTouch文件中的“ Horizo​​ntalCollectionViewCell”类内部)实现了相同的功能(以及其他功能,例如numberOfItemsAtIndexPath,cellForItemAt,sizeForItemAt等)。但全部如此。所有视图和子视图(包括两个集合视图)都是使用情节提要(而非代码)创建的。我过去1星期一直在寻找解决方案,但尚未成功。请帮助我找到解决该问题的合适方法。

这是ViewController文件中的代码:-

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

@IBOutlet weak var horizontalView: UIView! // Container View for horizontal Collection View.

@IBOutlet weak var horizontalCollectionView: UICollectionView!


override func viewDidLoad() {
    super.viewDidLoad()
    horizontalCollectionView.delegate = self
    horizontalCollectionView.dataSource = self
    menuBarCollectionView.delegate = self
    menuBarCollectionView.dataSource = self
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView == menuBarCollectionView {
        let cell = menuBarCollectionView.dequeueReusableCell(withReuseIdentifier: "menuButtonsCell", for: indexPath) as! MenuBarCollectionViewCell

        cell.menuButtonsLabel.text = menuButtonsName[indexPath.row]
        return cell
    }

    let cell = horizontalCollectionView.dequeueReusableCell(withReuseIdentifier: "horizontalCell", for: indexPath) as! HorizontalCollectionViewCell
    return cell
}


func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
    if collectionView == menuBarCollectionView {
        let menuButtonWidthFactor = self.menuBarView.frame.width - 1
        return CGSize(width: menuButtonWidthFactor / 3, height: 35)
    }
    return CGSize(width: horizontalView.frame.width, height: horizontalView.frame.height)

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    if collectionView == menuBarCollectionView {
        return 0.5
    }

    return 0
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        menuBarCollectionView.collectionViewLayout.invalidateLayout()
        horizontalCollectionView.collectionViewLayout.invalidateLayout()
}
}

以下是子类Horizo​​ntalCollectionViewCell文件中的代码:-

import UIKit

class HorizontalCollectionViewCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

@IBOutlet weak var verticalCollectionView: UICollectionView! {
   didSet {
        self.verticalCollectionView.delegate = self
        self.verticalCollectionView.dataSource = self
    }
}


@IBOutlet weak var verticalView: UIView!

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = verticalCollectionView.dequeueReusableCell(withReuseIdentifier: "verticalCell", for: indexPath)
    cell.backgroundColor = UIColor.red
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: self.verticalView.frame.width, height: 100)
}

func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    verticalCollectionView.collectionViewLayout.invalidateLayout()
}
}

0 个答案:

没有答案