Gas*_*sim 5 iphone ios uicollectionview swift
我有一个UICollectionView,它是整个视图,但它位于"视图"内(它不是UICollectionViewController).将单元格添加到此集合视图会在故事板中按以下顺序显示它:
这是它在模拟器中的外观:
我不明白如何摆脱这种观点.对于集合视图,Storyboard Size Inspector中的所有insets都为零.只是为了确定,我还有以下代码:
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)
}
Run Code Online (Sandbox Code Playgroud)
如何摆脱顶部填充?
Joe*_*Joe 11
您可以通过考虑以下方法之一来修复顶部填充问题.
方法1:通过dimensions正确设置collectionView来自然地解决您的问题StoryBoard.
方法2:**Updated**
您可以collection frame在viewDidLayoutSubviews或中验证viewWillLayoutSubviews
override func viewDidLayoutSubviews() {
collectionView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
}
Run Code Online (Sandbox Code Playgroud)
方法3:你可以Adjust Scroll View Insets从你的StoryBoard Attributes Inspector.
方法4:您可以通过调整CollectionView以编程方式解决此问题contentInset.
collectionView.contentInset = UIEdgeInsets(top: **Any Value**, left: 0, bottom: 0, right: 0)
Run Code Online (Sandbox Code Playgroud)
顶部填充5的输出:
顶部填充44的输出:
顶部填充64的输出:
还有另一种方法可以解决此问题,即选择 collectionView ->scrollView Content Insets ->“自动”为“从不”。
默认情况下,scrollView Content Insets 值为“自动”。请检查下图。
有关更多详细信息,请检查:UIScrollView.ContentInsetAdjustmentBehavior
https://developer.apple.com/documentation/uikit/uiscrollview/2902261-contentinsetadjustmentbehavior
| 归档时间: |
|
| 查看次数: |
4589 次 |
| 最近记录: |