Swift-如何针对不同的IOS设备大小调整UICollectionViewCell的比率

时间:2019-02-08 12:05:24

标签: swift uicollectionview uicollectionviewcell frame uicollectionviewflowlayout

我正在使用     CollectionView 作为我底部的菜单栏     ViewController 我创建了一个自定义类,并制作了5个均匀间隔的单元格,并用以下内容填充了iPhone 8上的整个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

我的问题是,即使需要CollectionView来增加其高度,如何将像元扩大到适合的大小,我希望将图像(将放置在像元中)在iPhone 8屏幕上保持其长宽比。

能否仅通过代码增加每个CollectionViewCell的大小? 如果可以的话,有没有办法解决目前发布的每部iPhone的宽度/高度问题?

或者有人可以推荐其他方法来完成此操作吗?如果我以错误的方式来处理这个问题。

谢谢!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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
}
}