【问题标题】:How to make UICollectionViewFlowLayout stay inside a UICollectionView如何使 UICollectionViewFlowLayout 留在 UICollectionView 内
【发布时间】:2014-10-20 07:59:43
【问题描述】:

我已经使用集合视图构建了一个自定义日历。我的问题是,当我添加UICollectionViewFlowLayout 时,它会覆盖整个屏幕并且不会留在UICollectionView 内。如何让UICollectionViewFlowLayout 留在UICollectionView 内?这是我的代码:

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 100, left: 10, bottom: 1, right: 10)
    let width = UIScreen.mainScreen().bounds.width
    layout.itemSize = CGSize(width: width/10, height: 35)

    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    collectionView!.dataSource = self
    collectionView!.delegate = self
    collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
    collectionView!.backgroundColor = UIColor.whiteColor()

    self.view.addSubview(collectionView!)

我希望日历在哪里:

这是它覆盖整个屏幕的方式:

【问题讨论】:

    标签: ios objective-c xcode swift uicollectionview


    【解决方案1】:

    这一行:

    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    

    正在创建一个新的 UICollectionView 实例,并将其框架设置为与self.view 相同(因此它覆盖了全屏)。我怀疑您实际上想要使用在故事板中建立的现有集合视图实例(并且可能正在呈现 - 但在新的后面!)。检查你的故事板,看看 CollectionView 是否连接到你的视图控制器的 collectionView 属性:

    如果是这样,您可以只注释掉上面的行(可能还有接下来的两行),因为链接将在您的视图控制器被实例化时建立。

    如果您没有情节提要实例,则只需修改框架以适合您想要的位置和大小,而不是使用self.view.frame

    【讨论】:

      猜你喜欢
      • 2019-11-06
      • 2016-08-21
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      • 2019-08-12
      • 2015-04-04
      • 2019-01-01
      • 1970-01-01
      相关资源
      最近更新 更多