问题描述
我有一个 UICollectionView,它是整个视图,但它位于视图"内(它不是 UICollectionViewController).向此集合视图添加一个单元格会在情节提要中按以下顺序显示它:
I have a UICollectionView that is the entire view but it lives inside "view" (it is not UICollectionViewController). Adding a cell to this collection view shows it in the following order in storyboard:
这是它在模拟器中的样子:
This is how it looks in the emulator:
我不明白如何摆脱这种看法.集合视图的 Storyboard Size Inspector 中的所有插图都为零.可以肯定的是,我还有以下代码:
I don't understand how to get rid of that view. All the insets are at zero in Storyboard Size Inspector for collection view. Just to be sure, I also have the following code:
override func viewWillLayoutSubviews() {
let layout = self.collectionViewProducts.collectionViewLayout as! UICollectionViewFlowLayout
let containerWidth = UIScreen.main.bounds.size.width - 40.0
let itemWidth = (containerWidth / 3.0)
let itemHeight = (itemWidth / 0.75) + 30
layout.sectionInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
layout.itemSize = CGSize(width: itemWidth, height: itemHeight)
}
我怎样才能摆脱顶部填充?
How can I get rid of that top padding?
推荐答案
您可以通过考虑以下方法之一来解决顶部填充问题.
You can fix top padding issue by considering one of the following method.
方法 1:通过从 StoryBoard
正确设置您的 collectionView dimensions
来解决问题的自然方法.
Method 1: Natural way to fix your problem by setting up your collectionView dimensions
properly from StoryBoard
.
方法二:**更新**
您可以在 viewDidLayoutSubviews
或 viewWillLayoutSubviews
override func viewDidLayoutSubviews() {
collectionView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
}
方法 3:您可以从 StoryBoard
Attributes Inspector
Adjust Scroll View Insets
.
方法 4:您可以通过调整 CollectionView contentInset
以编程方式解决此问题.
Method 4: You can fix this issue programatically by adjusting CollectionView contentInset
.
collectionView.contentInset = UIEdgeInsets(top: **Any Value**, left: 0, bottom: 0, right: 0)
顶部填充 5 的输出:
顶部填充 44 的输出:
顶部填充 64 的输出:
这篇关于uicollectionview 删除顶部填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!