简体   繁体   English

动态滚动视图

[英]Dynamic Scroll View

It's the same question with Simon's 西蒙的问题也一样

But i can't comment there because of 50 reputation and i have the exact same problem. 但由于50的声誉,我无法在此发表评论,而我也有完全相同的问题。 Sorry for re-post but there was no solution. 很抱歉重新发布,但没有解决方案。 Any help is appreciated. 任何帮助表示赞赏。 Thanks! 谢谢!

The Problem is.. 问题是..

I have a 我有一个

-UIScrollView -UIScrollView

---Labels - -标签

---Labels - -标签

---UICollectionView --- UICollectionView

I have disabled the UICollectionViews scrolling functionality, so I only want to scroll with the Scrollview. 我已禁用UICollectionViews滚动功能,所以我只想使用Scrollview滚动。 My problem is that I don't know how I can calculate the UICollectionViews height so I can update my UIScrollView. 我的问题是我不知道如何计算UICollectionViews的高度,因此无法更新UIScrollView。 If it's many elements inside the UICollectionView, it's get clipped. 如果UICollectionView中有很多元素,则会被裁剪。

I have created this little HTML example to show what I'm looking for http://jsfiddle.net/hDwPH/ 我创建了这个小HTML示例,以显示我在寻找什么http://jsfiddle.net/hDwPH/

Dummy

I suggest to to let it just one collectionView instead of creating this view hierarchy, it is not a good practice to add a scrollview inside another scrollview. 我建议只让它一个collectionView而不是创建此视图层次结构,在另一个滚动视图中添加滚动视图不是一个好习惯。

When implementing cellForItemAtIndexPath , you can check what's the current indexPath.item , based on it -let's say if it 0 or 1-, you can return a different custom cell that contains your the label(s). 在实现cellForItemAtIndexPath时 ,您可以根据它检查当前的indexPath.item假设它是0还是1-,则可以返回包含标签的其他自定义单元格。

For example: 例如:

Swift 2 迅捷2

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    // check depends on how many cells they should be to display the labels, i.e: one label for each cell or all of the labels in one cell:
    if indexPath.item == 0 {
        // retrun the cell with the label(s)
    }

    // return the regular cell...
}

It's better to use single CollectionView with different kind of cells. 最好将单个CollectionView与不同类型的单元格一起使用。

But if you need to use it as you described, you should change contentSize of UIScrollView. 但是,如果需要按说明使用它,则应更改UIScrollView的contentSize。 And frame of collectionView(or height constraint if you use automatic layout). 和collectionView的框架(如果使用自动布局,则为高度限制)。 But it's a dirty way 但这是一种肮脏的方式

collectionView.reloadData()
collectionView.layoutIfNeeded()
scrollview.contentSize.height = labelsHeight + collectionView.contentSize.height
collectionView.frame.size.height =     collectionView.contentSize.height

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

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