gpt4 book ai didi

ios - 如何使 UICollectionViewFlowLayout 留在 UICollectionView 中

转载 作者:行者123 更新时间:2023-11-28 05:33:26 25 4
gpt4 key购买 nike

我已经使用 Collection View 构建了一个自定义日历。我的问题是,当我添加 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!)

我想要日历的位置:

Where I want the calendar to be

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

enter image description here

最佳答案

这一行:

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

正在创建 UICollectionView 的新实例,并将其框架设置为与 self.view 相同(因此它覆盖了整个屏幕)。我怀疑您实际上想使用在 Storyboard 中建立的 Collection View 的现有实例(并且可能正在呈现 - 但在新的之后!)。检查你的 Storyboard,看看 CollectionView 是否连接到你的 View Controller 的 collectionView 属性:

Screenshot

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

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

关于ios - 如何使 UICollectionViewFlowLayout 留在 UICollectionView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26460905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com