【问题标题】:Set The CollectionView As Banner将 CollectionView 设置为横幅
【发布时间】:2019-05-10 08:46:57
【问题描述】:

我必须在我的视图控制器中收集视图。一个是显示图像,另一个是显示票的详细信息。这是示例图片。

但我无法设置适当的约束。这就是我对代码所做的事情。

 let numberOfCells = 9
    let kCellHeight : CGFloat = 104
    let kLineSpacing : CGFloat = 2
    let kInset : CGFloat = 10

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if collectionView == clnView {
            return CGSize.init(width: width, height: height)
        }else {
            return CGSize(width: (UIScreen.main.bounds.width - 2*kInset - kLineSpacing), height: kCellHeight)
        }
    }
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        var size = CGFloat()
        if collectionView == clnView {
            size = kLineSpacing
        }
        return size
    }
 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        var Insect = UIEdgeInsets()
        if collectionView == clnView {
            Insect = UIEdgeInsets(top: kInset, left: kInset, bottom: kInset, right: kInset)
        }
        return Insect
    }

但它没有正确显示。看看

iphone 5s

iPhone XR

请解释正确的方法。谢谢

【问题讨论】:

    标签: swift uicollectionview autolayout constraints


    【解决方案1】:

    您的代码只是定义了项目在集合视图中的布局方式。

    约束应该告诉您父视图集合视图的放置位置,以及宽度和高度。

    您必须 learn autolayout 并在您的故事板或 xib 中逐步执行此操作以获取视图。使用适当的 Y 约束,它应该按预期工作。如果您将collectionview + 其他内容放在滚动视图中,请关注this answer

    【讨论】:

      猜你喜欢
      • 2014-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多