uicollectionview删除顶部填充 [英] uicollectionview remove top padding
问题描述
我有一个UICollectionView,它是整个视图,但它位于view内(它不是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中的所有insets都为零。为了确定,我还有以下代码:
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 维度
来自然地解决您的问题。
Method 1: Natural way to fix your problem by setting up your collectionView dimensions
properly from StoryBoard
.
方法2 : **已更新**
您可以在 viewDidLayoutSubviews
或 viewWillLayoutSubviews <中验证
集合框架
/ code>
You can validate collection frame
in viewDidLayoutSubviews
or viewWillLayoutSubviews
override func viewDidLayoutSubviews() {
collectionView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
}
方法3 :您可以从 StoryBoard
<调整滚动视图插入
code>属性检查员。
Method 3: You can Adjust Scroll View Insets
from your StoryBoard
Attributes Inspector
.
方法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删除顶部填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!