简体   繁体   English

iOS:自适应集合视图单元格宽度

[英]iOS : Adaptive Collection View cell width

I'm trying to make a grid view on iOS with adaptive column width. 我正在尝试在iOS上使用自适应列宽创建网格视图。 On Android it's possible to do this by setting stretchMode attribute to spacingWidth . 在Android上,可以通过将stretchMode属性设置为stretchModespacingWidth This attribute take the cell width as minimum width and grow cell automatically if there is free space available but no enough to add another column keeping the same space between column everywhere. 此属性将像元宽度作为最小宽度,如果有可用空间,但不足以添加另一列,并在各处保持列之间的相同空间,则自动增大像元。

I didn't found any way to do that on iOS. 我没有找到在iOS上执行此操作的任何方法。

不称职的截图

It look like that (left image) on iPhone 6, but on iPhone 5 (right image) the space is very big and ugly. 在iPhone 6上看起来像这样(左图),但是在iPhone 5上(右图),空间非常大且难看。 I wan't to auto resize cells to avoid this big space. 我不会自动调整单元格的大小,以免出现这么大的空间。

How can i do that on iOS ? 如何在iOS上执行此操作?

(I'm using Xcode 6.1) (我正在使用Xcode 6.1)

Thanks 谢谢

EDIT : This i why i wan't (black space is approximatively desired additional cell width, sorry my draw is ugly I did it quickly) 编辑:这就是为什么我不想要(黑色空间大约是期望的其他单元格宽度,对不起,我的绘制很丑,我很快就做到了) 在此处输入图片说明

EDIT 2: 编辑2:

I tried to calculate new size with this code, but the result is "strange" (wrong size, position) , i think i missed something 我试图用此代码计算新的大小,但结果是“奇怪”(错误的大小,位置),我想我错过了一些东西

    let CELL_SIZE : Float = 92
    let CELL_MARGIN : Float = 10
    let COLLECTION_VIEW_MARGIN : Float = 20 //Left right margin
    let screenWidth = Float(UIScreen.mainScreen().bounds.width)
    let numberOfCell =  Float(Int(screenWidth / (CELL_SIZE + CELL_MARGIN + COLLECTION_VIEW_MARGIN)))

    let oldCellSize = Float(cell.frame.width)
    var newCellSize : Float
    if(numberOfCell >= 2){
        newCellSize = (screenWidth / numberOfCell) - (CELL_MARGIN * (numberOfCell-1))
    } else {
        newCellSize = (screenWidth / numberOfCell)        }

    let indexPathRow = Float(indexPath.row)

    var xOffsetMultiplier = indexPathRow % numberOfCell
    if(xOffsetMultiplier == 0){
        xOffsetMultiplier = numberOfCell
    }

    var newX : Float = 0
    if(xOffsetMultiplier == 1){
        newX = COLLECTION_VIEW_MARGIN / 2
    } else {
        newX = (newCellSize + CELL_MARGIN) * (xOffsetMultiplier-1) + (COLLECTION_VIEW_MARGIN / 2)
    }

    var frame = CGRectMake(CGFloat(newX), cell.frame.minY, CGFloat(newCellSize), cell.frame.height)

    cell.frame = frame

This code is written in func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell in my ViewController 这段代码写在func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell我的ViewController中的func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell

您应该使用UICollectionViewFlowLayout根据集合视图大小采用单元格大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM