-1

CollectionView 在底部使用 a 作为菜单栏, 我创建了一个自定义类,并制作了 5 个均匀间隔的单元格,这些单元格在 iPhone 8 上ViewController 填充了以下内容,这是预期的结果 -CollectionView

    layout.itemSize = CGSize(width: self.frame.width / 5, height: self.frame.height)
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0

    self.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    self.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    self.collectionViewLayout = layout;

但是,当我在任何具有更大屏幕的 iPhone 上运行该项目时,我得到以下结果:

集合视图

我的问题是,我怎样才能将单元格放大以适应没有任何边距,即使它需要CollectionView增加它的高度,我希望图像(将放置在单元格中)保持它们与 iPhone 8 的纵横比屏幕。

我可以CollectionViewCell用代码增加每个的大小吗?如果是这样,有没有办法在不计算迄今为止发布的每部 iPhone 的宽度/高度的情况下这样做?

或者有人可以推荐一种不同的方法来实现这一点吗?如果我以错误的方式接近这个。

提前致谢!

4

2 回答 2

1

看起来视图的宽度在你放线的地方不正确

layout.itemSize = CGSize(width: self.frame.width / 5, height: self.frame.height)

所以用

layout.itemSize = CGSize(width: UIScreen.main.bounds.width / 5, height: self.frame.height)

你也可以实现

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

符合UICollectionViewDelegateFlowLayout

于 2019-02-08T12:18:51.297 回答
1

这是我的 CollectionView 代码。您可以根据您的要求进行修改。

class HomeViewController: UIViewController,UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {


@IBOutlet weak var homeCollectionView: UICollectionView!


override func viewDidLoad() {
    super.viewDidLoad()

    homeCollectionView.delegate = self;
    homeCollectionView.dataSource = self;
    homeCollectionView.reloadData()
}


//MARK:- COLLECTION VIEW
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 3
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
    if let count = myArray.count{
            return count
    }
    return 0
}

func collectionView(_ collectionView: UICollectionView,
                    viewForSupplementaryElementOfKind kind: String,
                    at indexPath: IndexPath) -> UICollectionReusableView {

    switch kind {
    case UICollectionElementKindSectionHeader:
        let headerView = homeCollectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HomeHeaderCell", for: indexPath) as! HomeHeaderCell
        headerView.UpdateHeaderCell(indexPath: indexPath)
        headerView.btn_SeeAll.addTarget(self, action: #selector(SeeAllDestinations), for: .touchUpInside)
        return headerView
    case UICollectionElementKindSectionFooter:
        let footerView = homeCollectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HomeFooterCell", for: indexPath) as! HomeFooterCell
        return footerView
    default:
        return UICollectionReusableView()
    }
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: collectionView.frame.width, height: 80)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
    return CGSize(width: collectionView.frame.width, height: 10)
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
     let myCell:HomeTopDestinationsCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeTopDestinationsCell", for: indexPath) as! HomeTopDestinationsCell
        myCell.backgroundColor = UIColor.colorFromCode(0xf1f1f1)
        myCell.UpdateCellValue(obj:(myArray[indexPath.row])!)
        return myCell as HomeTopDestinationsCell;
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        }

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

            if DeviceType.IS_IPAD{
            return CGSize(width: (self.view.frame.width-60)/3 , height: (self.view.frame.width-60)/3);
        }
        return CGSize(width: (self.view.frame.width-60)/2 , height: (self.view.frame.width-60)/2);
}
//Use for interspacing
func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 10.0
}

func collectionView(_ collectionView: UICollectionView, layout
    collectionViewLayout: UICollectionViewLayout,
                    minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 10.0
}
}
于 2019-02-08T12:38:43.207 回答