简体   繁体   English

如何根据 ios 中的集合视图行数使滚动视图增长?

[英]how to make scrollview grow based on collection view number of rows in ios?

i have a scrollview which has a picture, text, button, label and then a collectionView whose number of rows is dynamic in nature.我有一个滚动视图,它有一个图片、文本、按钮、label,然后是一个行数本质上是动态的 collectionView。 Its like collectionview will grow in height and hence the scrollview should assume collectionView height as well as other elements height to get a smooth scroll.它像 collectionview 的高度会增加,因此 scrollview 应该假定 collectionView 高度以及其他元素的高度以获得平滑的滚动。 How to achieve this using auto Layout in ios?如何在 ios 中使用自动布局来实现这一点?

So my doubt is how to make a dynamic scrollview height based on a growing colelctionview or tableview height.所以我的疑问是如何根据不断增长的 colelctionview 或 tableview 高度制作动态滚动视图高度。

You can increase the height of CollectionView Height Constraint , connect Height contains to your ViewController and than .您可以增加CollectionView Height Constraint 的高度,将 Height contains 连接到您的ViewController和 than 。

how to connect constraint to outlet ?如何将约束连接到插座?

let height = self.collectionView.contentSize.height;
self.heightConstraints = height;

self.scrollView.contentSize = CGSize.init(widht:self.scrollView.frame.size.width,height:height)

FOr Swift 4.2对于斯威夫特 4.2

override func viewDidLayoutSubviews() {
    let heightFilterCOll = self.filterCollectionView.contentSize.height
    self.filterCOllectionViewCOnstraintHeight.constant = heightFilterCOll

    self.ScrollView.contentSize = CGSize(width:self.ScrollView.frame.size.width,height:heightFilterCOll + 77)
}

These methods didn't work for me.这些方法对我不起作用。 Try,尝试,

self.collectionView.reloadData()
DispatchQueue.main.async {
    self.heightCollectionViewConstraint.constant = self.collectionViews.collectionViewLayout.collectionViewContentSize.height
}

@ Wimukthi Rajapaksha @Wimukthi Rajapaksha

try his solutions its working perfectly.试试他的解决方案,它工作得很好。 Just keep in mind to specify a height of collection in scroll view.请记住在滚动视图中指定集合的​​高度。 create an outlet of the height constant.创建一个高度常数的出口。 Reload table and assign the dynamic height.重新加载表格并分配动态高度。 Thanks.谢谢。

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

相关问题 在iOS 8中将集合或表格视图或自定义视图添加到滚动视图 - Adding a collection or tableview or custom view to a scrollview in ios 8 如何制作两行水平集合视图? - how to make two rows horizontal collection view? 集合视图在设置行数后水平滚动? iOS Swift - collection view horizontal scroll after set number of rows ? ios Swift 如何使当前视图成为滚动视图? iOS /目标C - How to make current view a scrollview? iOS / Objective C 如何在 ios 中创建没有表视图和集合视图的行 - How can I create rows without tableview and collection view in ios 如何基于一个月中的某天在集合视图中创建一定数量的按钮 - How to create a certain number of buttons in a collection view based on days of the month iOS UICollectionView-如何使第一个单元格出现在集合视图的底部? - iOS UICollectionView - how to make first cell appear at the bottom of the collection view? 如何使图像视图与iOS中的自动布局成比例地增长? - How to make imageviews that proportionally grow with auto layout in iOS? 具有固定行数和列数的集合视图 - Collection View With Fixed Number of Rows and Columns iOS 9:如何使滚动条底部的内容视图固定在scrollview底部? - iOS 9 : how to make the content view of a scroll bar bottom stick to the scrollview bottom?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM