Xcode UICollectionView单元格旋转后不水平

Xcode UICollectionView单元格旋转后不水平,xcode,swift,uicollectionview,Xcode,Swift,Uicollectionview,我有一个UICollectionView,上面有一个创建单元格的按钮,它应该按照创建的顺序显示(如果空间允许,1,2,3,4横穿和向下)。文本视图以灵活的宽度进行约束,以填充单元格。单元格大小取决于设备和旋转,每行允许1、2、3或4个单元格,如下所示: func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtInd

我有一个UICollectionView,上面有一个创建单元格的按钮,它应该按照创建的顺序显示(如果空间允许,1,2,3,4横穿和向下)。文本视图以灵活的宽度进行约束,以填充单元格。单元格大小取决于设备和旋转,每行允许1、2、3或4个单元格,如下所示:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
    var size = collectionView.bounds.size       // size of collection view

    switch size.width
    {
    case 0...450:
        println(size.width)
        return CGSize(width: (size.width - 10), height: 145)
    case 451..<768:
        println(size.width)
        return CGSize(width: ((size.width - 10) / 2), height: 145)
    case 768:
        println(size.width)
        return CGSize(width: ((size.width - 10) / 3), height: 145)
    default:
        println(size.width)
        return CGSize(width: 248, height: 150)      // in case there is no size
    }
}
func collectionView(collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeFormiteIndepath indepath:nsindepath)->CGSize
{
var size=collectionView.bounds.size//集合视图的大小
开关大小和宽度
{
案例0…450:
println(大小、宽度)
返回CGSize(宽度:(size.width-10),高度:145)
判例451..Int
{
返回1
}
重写func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int
{
返回单元
}
重写func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell
{
让cell=collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier,forIndexPath:indexPath)作为UICollectionViewCell
返回单元
}
重写func collectionView(collectionView:UICollectionView,ViewForSupplementElementOfKind种类:String,atIndexPath indexPath:NSIndexPath)->UICollectionReusableView
{
开关种类//布局提供的辅助视图种类
{
案例UICollectionElementKindSectionFooter://节头已在情节提要中选择
让footerView=collectionView.dequeueReusableSupplementaryViewOfKind(kind,带有ReuseIdentifier:“CountFooterView”,forIndexPath:indexPath)作为UICollectionReusableView//deque并选择正确的页眉
返回页脚视图
违约:
断言(false,“意外元素种类”)
}
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,SizeFormiteIndeXPath indexPath:NSIndexPath)->CGSize
{
变量大小=collectionView.bounds.size
开关大小和宽度
{
案例0…450:
返回CGSize(宽度:(size.width-20),高度:145)
案例451.指南
{
返回部分插图
}

}

是否将节数设置为1,如果未设置为1,请重试。

是否将节数设置为1,如果未设置为1,请重试。

编号33=项目计数! 您可以独立于项目总数对其进行调整,以适应屏幕中更多或更少的项目!例如,iPhone 4上的/(Double(33))将适合4列33个项目,每行4个项目!在iPhone 6 Plus中,您将看到相同的图片单元格将按4列放大

// ...........returning cell size based on device screen size by calculating square roo

        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

            let deviceSize = UIScreen.mainScreen().bounds.size
            let cellSize = sqrt(Double(deviceSize.width * deviceSize.height) / (Double(33)))

                return CGSize(width: cellSize , height: cellSize)

        }
编号33=物品计数! 您可以独立于项目总数对其进行调整,以适应屏幕中更多或更少的项目!例如,iPhone 4上的/(Double(33))将适合4列33个项目,每行4个项目!在iPhone 6 Plus中,您将看到相同的图片单元格将按4列放大

// ...........returning cell size based on device screen size by calculating square roo

        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

            let deviceSize = UIScreen.mainScreen().bounds.size
            let cellSize = sqrt(Double(deviceSize.width * deviceSize.height) / (Double(33)))

                return CGSize(width: cellSize , height: cellSize)

        }

你能提供源代码并解释其工作原理吗?是的,指定了第1节。我在原始帖子的底部追加了完整的VC代码。你能提供源代码并解释其工作原理吗?是的,指定了第1节。我在原始帖子的底部追加了完整的VC代码。你好,Ruso,代码不错,到最大化方形单元格的大小,然后在添加更多单元格时将其缩小。对于给定的设备和旋转,我的单元格的大小是恒定的。我的问题是,除非在设备处于横向位置时添加单元格,否则大小设置似乎无法正确应用!嗨,Ruso,代码不错,将方形单元格的大小最大化,然后缩小它们随着添加的更多。对于给定的设备和旋转,我的单元格的大小是恒定的。我的问题是,除非在设备处于横向位置时添加单元格,否则大小设置似乎无法正确应用!