gpt4 book ai didi

Swift UICollectionViewLayout,其中 contentSize

转载 作者:行者123 更新时间:2023-11-30 11:38:39 26 4
gpt4 key购买 nike

我正在将我的应用程序从 Obj-C 转换为 Swift。

我有 UICollectionViewLayout 的子类,它覆盖了已重命名为“prepare”的“prepareForLayout”。到目前为止,一切都很好。但是,在我的 obj-c 代码中,我有这一行:

self.contentSize = CGSizeMake(contentWidth, contentHeight+40);

其中“self”指的是自定义 UICollectionViewLayout。

现在,在 Swift 中,当我尝试这样做时

self.contentSize = CGSize(width: contentWidth, height: contentHeight+40)

我收到此错误声明

Value of type 'myCustomCollectionLayout' has no member 'contentSize'

我确实知道有类似 collectionViewContentSize 的东西,但这似乎不适合这里。

我希望你们中的任何人都可以建议我如何继续

最佳答案

contentSize 不是 UICollectionViewLayout 属性,它是 UICollectionViewUIScrollView 上的属性。

    collectionView?.contentSize = CGSize(width: contentWidth, height: contentHeight+40)

但是,为什么在 CollectionView 布局中需要这个。您确实应该考虑覆盖属性 collectionViewContentSize,这正是 contentSize 对 UIScrollView 所做的事情,

override var collectionViewContentSize: CGSize {
return CGSize(width: contentWidth, height: contentHeight+40)
}

关于Swift UICollectionViewLayout,其中 contentSize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49462730/

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