1
votes

After so much trouble i am able to scroll the scrollview, and it is working fine. Now my question is how to set the dynamic height of scrollview and main view after adding view inside scroll view. As adding height from IB make it static and inside content height may very.

steps that i followed are.

  1. Added a uiview controller inside my storyboard.
  2. Then inside this uiview added one uiscrollview.
  3. Set the height of the main view to 1000 and scrollview to 1000.
  4. Selected the uiscrollview and reset it to suggested constraint.
  5. In this uiscrollview i added many labels, uiviews, textviews, etc programatically.

Now the main isuue is this 1000 height that i set in step 3 is static and the content is dynamic, so how to set height dynamically. and also is it necessary to follow step 4, when i am doing it programatically.

1

1 Answers

0
votes

If you know the new subview's height, you can change the frame size by

// Add your subview to scroll view
...
// Set new height
CGRect newScrollFrame;
newScrollFrame.origin = MyScrollView.frame.origin;
newScrollFrame.size = CGSizeMake(MyScrollView.frame.size.width, MyScrollView.frame.size.height + MyNewSubView.frame.size.height);
MyScrollView.frame = newScrollFrame;