简体   繁体   English

uicollectionview 删除顶部填充

[英]uicollectionview remove top padding

I have a UICollectionView that is the entire view but it lives inside "view" (it is not UICollectionViewController).我有一个 UICollectionView 是整个视图,但它位于“视图”中(它不是 UICollectionViewController)。 Adding a cell to this collection view shows it in the following order in storyboard:向这个集合视图添加一个单元格在故事板中按以下顺序显示它:

故事板视图

This is how it looks in the emulator:这是它在模拟器中的样子:

模拟器视图

I don't understand how to get rid of that view.我不明白如何摆脱这种观点。 All the insets are at zero in Storyboard Size Inspector for collection view.在 Storyboard Size Inspector 中,集合视图中的所有插图都为零。 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.您可以通过考虑以下方法之一来解决顶部填充问题。

Method 1 : Natural way to fix your problem by setting up your collectionView dimensions properly from StoryBoard .方法 1 :通过从StoryBoard正确设置 collectionView dimensions来解决问题的自然方法。

在此处输入图片说明

Method 2 : **Updated**方法 2**Updated**

You can validate collection frame in viewDidLayoutSubviews or viewWillLayoutSubviews您可以在viewDidLayoutSubviewsviewWillLayoutSubviews验证collection frame

  override func viewDidLayoutSubviews() {
     collectionView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
}

Method 3 : You can Adjust Scroll View Insets from your StoryBoard Attributes Inspector .方法 3 :您可以从StoryBoard Attributes Inspector Adjust Scroll View Insets

在此处输入图片说明

Method 4 : You can fix this issue programatically by adjusting CollectionView contentInset .方法 4 :您可以通过调整 CollectionView contentInset编程方式修复此问题。

collectionView.contentInset = UIEdgeInsets(top: **Any Value**, left: 0, bottom: 0, right: 0)

Output with top padding 5:顶部填充 5 的输出:

在此处输入图片说明

Output with top padding 44:顶部填充 44 的输出:

在此处输入图片说明

Output with top padding 64:顶部填充 64 的输出:

在此处输入图片说明

I think because this viewController is embedded in a navigationController.我想是因为这个 viewController 嵌入在一个 navigationController 中。 Let select this viewController in the storyboard and uncheck Adjust Scroll View Insets :让我们在故事板中选择这个 viewController 并取消选中Adjust Scroll View Insets

在此处输入图片说明

通过取消选中 IB > Attribute Inspector 中的Translucent复选框,使您的Navigation Controller > NavigationBar变为半透明,它将起作用。

There is one more way to resolve this issue and that is selecting collectionView -> scrollView Content Insets -> "Automatic" to "Never".还有一种方法可以解决这个问题,那就是选择 collectionView -> scrollView Content Insets -> “Automatic” 到 “Never”。

By default scrollView Content Insets value is Automatic.默认情况下,scrollView 内容插入值为自动。 Please check the below image.请检查下图。

For more details check: UIScrollView.ContentInsetAdjustmentBehavior有关更多详细信息,请检查: UIScrollView.ContentInsetAdjustmentBehavior

https://developer.apple.com/documentation/uikit/uiscrollview/2902261-contentinsetadjustmentbehavior https://developer.apple.com/documentation/uikit/uiscrollview/2902261-contentinsetadjustmentbehavior

在此处输入图片说明

I also had the same problem, and i fixed it with a way totally ridiculous solution.我也遇到了同样的问题,我用一种完全荒谬的解决方案修复了它。

My collectionView contained several sections which had no title & no item cells.我的 collectionView 包含几个没有标题和项目单元格的部分。

The top, bottom inset values of the section insets were 10 respectively.截面插图的顶部、底部插图值分别为 10。 so each empty section charged height of 20 pixels.所以每个空白部分的高度为 20 像素。

I had 4 empty sections, and therefore, 80 top margins in the collection view.我有 4 个空白部分,因此在集合视图中有 80 个上边距。

Hope you check this as well if none of the above solutions works.如果上述解决方案均无效,希望您也检查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM