how to make scrollview grow based on collection view number of rows in ios?
Asked Answered
F

3

1

i have a scrollview which has a picture, text, button, label and then a collectionView whose number of rows is dynamic in nature. 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. How to achieve this using auto Layout in ios?

So my doubt is how to make a dynamic scrollview height based on a growing colelctionview or tableview height.

Fimbriate answered 27/4, 2018 at 13:0 Comment(5)
Collection view implements UIScrollView by itself. The content size will adapt to the content you will provideZipnick
Are you using AutoLayout?Dellora
Do you want to increase the content size ?Lollipop
my viewcontroller has a headerview, image, text(long) followed by collection view which grows i want my entire content to be scrollable.. not just the collectionview. Yes using autolayout.Fimbriate
see the updated answerLollipop
L
1

You can increase the height of CollectionView Height Constraint , connect Height contains to your ViewController and 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)
Lollipop answered 27/4, 2018 at 13:11 Comment(0)
F
1

These methods didn't work for me. Try,

self.collectionView.reloadData()
DispatchQueue.main.async {
    self.heightCollectionViewConstraint.constant = self.collectionViews.collectionViewLayout.collectionViewContentSize.height
}
Foreordination answered 9/12, 2021 at 6:23 Comment(0)
L
0

FOr Swift 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)
}
Lorrimor answered 6/12, 2018 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.