【问题标题】:Scrolling collection view with UIViews on top?顶部带有 UIViews 的滚动集合视图?
【发布时间】:2016-12-19 21:51:55
【问题描述】:

我有一个 UICollectionViewController,我添加了两个 UIViews 作为子视图。一个是紫色的 UIView,上面是另一个白色的 UIView,蓝色的集合视图在两者后面向上滚动。

在这些 UIView 下方,我从前 300 个中创建了 collectionView.contentInset(这是两个 UIView 高度的总大小)。我想要完成的是将集合视图与上面的两个 UIView 一起滚动。它几乎与此线程 (Move a view when scrolling in UITableView) 上的解决方案相似,但当我覆盖 scrollViewDidScroll 时,整个框架向上滚动并且单元格位于两个视图后面。我想要的只是向上滚动 UIView,然后滚动浏览集合视图。我觉得这可能涉及嵌套滚动视图。

这就是我覆盖 scrollViewDidScroll 的方式:

    var rect = self.view.frame
    rect.origin.y =  -scrollView.contentOffset.y - 300
    self.view.frame = rect

编辑:我发布了一个视频,演示了我想要为每个 iOS Tumblr 应用程序做什么:https://youtu.be/elfxtzoiHQo

【问题讨论】:

标签: ios swift uiview uiscrollview uicollectionview


【解决方案1】:

我通过以下一些基本步骤实现了相同的要求。

//Declare the view which is going to be added as header view
    let requiredView = UIView()

//Add your required view as subview of uicollectionview backgroundView view like as
collectionView.backgroundView = UIView() 
collectionView.backgroundView?.addSubview(requiredView)

//After that control the frame of requiredHeaderView in scrollViewDidScroll delegate method like
        func scrollViewDidScroll(scrollView: UIScrollView) {
         let per:CGFloat = 60 //percentage of required view to move on while moving collection view
                let deductValue = CGFloat(per / 100 * requiredView.frame.size.height)
                let offset = (-(per/100)) * (scrollView.contentOffset.y)
                let value = offset - deductValue
                let rect = requiredView.frame
                self.requiredView.frame = CGRectMake(rect.origin.x, value, rect.size.width, rect.size.height)
        }

【讨论】:

  • requiredHeaderView 和 requiredView 是一样的吧?我相信你在那里打错了
  • @snapchatdotcom 对不起!你是对的。我已经编辑了答案,请检查。
【解决方案2】:

听起来你想要的是一个标题。

您可以使用以下任一方法为标题指定一个类或 nib:

self.collectionView.registerClass(_:, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier:)

registerNib(_:, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: )

如果您使用流式布局,您还应该指定参考高度:self.flowLayout.headerReferenceHeight = ...

然后您可以通过您的 UICollectionViewController 在collectionView(_:, viewForSupplementaryElementOfKind:, at:) 中通过检查节标题类型来提供标题。

这是一个不错的教程供参考:https://www.raywenderlich.com/78551/beginning-ios-collection-views-swift-part-2

【讨论】:

  • 如果我想做水平滚动,在页眉底部有一个空白的黑色视图,然后右边有单元格。这就是为什么我问是否有办法同时滚动 UICollectionView 作为内部滚动视图,然后是外部滚动视图。所有这一切,而滚动视图没有奇怪的行为。这基本上就是我在 gif 中所做的。
  • 我明白了,动画和视频都没有显示任何水平滚动。也许您应该更新它们以显示您的意思
【解决方案3】:

你有一个图书馆CSStickyHeaderFlowLayout

来自自述文件:

UICollectionView 替换 UITableView。做更多像视差标题,粘性部分标题。专为 iOS 7 设计。

【讨论】:

    【解决方案4】:

    试试这个。

    headerViewYConstraint 是标题视图的顶部 y 约束。

    存储最后的接触偏移量。

    var lastContentOffset: CGFloat = 0
    
    
    override func scrollViewDidScroll(scrollView: UIScrollView) {
        if scrollView != contentScrollView {
    
            if scrollView.dragging || scrollView.decelerating {
                let newOffset = scrollView.contentOffset.y
                let headerViewHeight = headerView.frame.width
                if headerViewHeight > 0 && scrollView.contentSize.height > view.frame.height + headerViewHeight{
                    var topOffset = newOffset == 0 ? 0.0 : (headerViewYConstraint.constant + lastContentOffset - newOffset)
                    topOffset = min(0, max(topOffset, -headerViewHeight))
                    if headerViewYConstraint.constant > topOffset || newOffset < headerViewHeight || lastDirectionalContentOffset - newOffset > cellHeight(){
                        headerViewYConstraint.constant = topOffset
                    }
                } else {
                    headerViewYConstraint.constant = 0
                }
                lastContentOffset = newOffset
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 2017-06-26
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多