【问题标题】:uicollectionview remove top paddinguicollectionview 删除顶部填充
【发布时间】:2017-03-26 00:00:44
【问题描述】:

我有一个 UICollectionView,它是整个视图,但它位于“视图”内(它不是 UICollectionViewController)。向此集合视图添加一个单元格会在情节提要中按以下顺序显示它:

这是它在模拟器中的样子:

我不明白如何摆脱这种看法。集合视图的 Storyboard Size Inspector 中的所有插图都为零。可以肯定的是,我还有以下代码:

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)
}

我怎样才能摆脱顶部填充?

【问题讨论】:

  • 你试过添加self.collectionView.contentInset = UIEdgeInsetsZero self.collectionView.scrollIndicatorInsets = UIEdgeInsetsZero。对不起 Swift 2.3 语法
  • 检查我的更新答案...

标签: ios iphone swift uicollectionview


【解决方案1】:

您可以通过考虑以下方法之一来解决顶部填充问题。

方法 1:通过从 StoryBoard 正确设置您的 collectionView dimensions 来解决您的问题的自然方法。

方法二**Updated**

您可以在viewDidLayoutSubviewsviewWillLayoutSubviews 中验证collection frame

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

方法3:你可以从你的Adjust Scroll View InsetsAdjust Scroll View InsetsAttributes Inspector

方法 4:您可以通过调整 CollectionView contentInset 以编程方式解决此问题。

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

顶部填充 5 的输出:

顶部填充 44 的输出:

顶部填充 64 的输出:

【讨论】:

    【解决方案2】:

    我认为是因为这个 viewController 是嵌入在一个 navigationController 中的。让我们在 storyboard 中选择这个 viewController 并取消选中 Adjust Scroll View Insets

    【讨论】:

      【解决方案3】:

      还有另一种方法可以解决此问题,即选择 collectionView -> scrollView Content Insets -> 从“自动”改为“从不”。

      默认情况下,scrollView Content Insets 值为 Automatic。请检查下图。

      更多详情请查看:UIScrollView.ContentInsetAdjustmentBehavior

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

      【讨论】:

      • 一个很好的解决方案
      【解决方案4】:

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

      【讨论】:

      • 为此苦苦挣扎了太久,这就是解决方法!
      【解决方案5】:

      我也遇到了同样的问题,我用一种完全荒谬的解决方案解决了这个问题。

      我的 collectionView 包含几个没有标题和没有项目单元格的部分。

      section insets 的顶部和底部插入值分别为 10。 所以每个空白部分的充电高度为 20 像素。

      我有 4 个空白部分,因此在集合视图中有 80 个顶部边距。

      如果上述解决方案都不起作用,希望您也检查一下。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-18
        • 1970-01-01
        • 2018-04-01
        相关资源
        最近更新 更多