0

I have created a scrollView programmatically which has a lot and complicated subviews based on some request I make. I know i can set scrollView's height by doing this scrollView.contentSize.height = sum of the height of subViews it has But my problem is it is difficult for me to keep track of the number of subViews that comes to be adding their height for the scrollView to use. My question is there a way I can set the height without having to keep track of available subViews it has?

chevi99
  • 123
  • 1
  • 2
  • 17
  • 1
    sounds like you need to look at using autolayout to set the height of your scrollview based on its content – sam-w Oct 18 '18 at 12:02
  • Why is it difficult to track? Also, check this [answer](https://stackoverflow.com/a/17283653/8558606) – Guilherme Matuella Oct 18 '18 at 12:07
  • @GuilhermeMatuella the link you provided when I implement it affects the width too what I want is the vertical height to be dynamic. – chevi99 Oct 18 '18 at 15:34
  • @chevi99 - are you using auto-layout at all? Scroll views with dynamic content become very, very easy to manage when using auto-layout and constraints. – DonMag Oct 18 '18 at 18:26
  • @DonMag yes I am. Actually I created all my views programmatically so yes I am using auto layout. This is how i set the height of my scrollView ie: scrollView.contentSize.height = sum(of all available views height). But my issues is now I am creating the views based on response from the server Example some response will add buttons some will add labels and other views. So i just need a best way to set a dynamic scrollView height. – chevi99 Oct 19 '18 at 12:08
  • @chevi99 - auto-layout can handle the contentSize *without* the need to explicitly calculate and set it. Here is a complete example of programmatically adding views to a scroll view (can be run directly in a playground page): https://stackoverflow.com/a/44933358/6257435 (the question indicated Swift 3, but it will run in Swift 4 without any changes). – DonMag Oct 19 '18 at 13:07
  • @DonMag the page u added works only if u have views that don't extend to the bottom of the superView what if u have a lot of UIViews UILabels a whole lot that the screen can show u have to set contentSize.height for the scrolling to work. – chevi99 Oct 21 '18 at 00:14
  • @chevi99 - The example on that page places a view 1000-pts from the top to demonstrate that it scrolls *without* explicitly setting `.contentSize`. Did you try running that example to see that scrolling works just fine? – DonMag Oct 21 '18 at 12:34

0 Answers0