mike vorisis
mike vorisis

Reputation: 2832

Dynamic Scrollview Swift

I have made a Button which generates TextFields and I want a ScrollView so if I'd add too much TextFields I could scroll and write to them. I also have a Button at the bottom of my view and I want this to be visible all the time. (I don't mind if only TextFields will be inside the ScrollView or the whole view except my bottom button).

This is how it looks like:

Upvotes: 1

Views: 5112

Answers (2)

Daniel
Daniel

Reputation: 377

You have to set the height constraint on your TextFields. Without that, the scrollView can't figure out its contentSize. After you set the heights, the scrollView will adapt accordingly.

Also, I would use a UIView and give a fix height for it and place the TextFields inside of the UIView.

You can also use scrollView.contentSize = CGSize(width: expectedWidth, height: expectedHeight)

Upvotes: 4

Shobhakar Tiwari
Shobhakar Tiwari

Reputation: 7892

These steps help you to design screen according to your requirement :

  1. Drag a ScrollView and provide height and width accordingly
  2. Decrease height of ScrollView by height of Button which you always want to show on bottom of page i.e- Button should be outside of ScrollView not within the ScrollView
  3. Adjust button size and height and put it below to ScrollView
  4. Now set Constraint to each one . First of all select ScrollView provide its l Left , Right , Top constraint .
  5. Select ScrollView and by pressing Control + drag mouse to Button ( below of Scrollview) and set vertical spacing between ScrollView and Button
  6. Select Button and provide Left , Right , Bottom Constraint.

Note - As AutoLayout works differently with ScrollView so always set constraint of all control within it from Order : Top To Bottom

Hope it helps you to fix this issue . Feel free to comment if still any confusion.

Thanks.

Upvotes: 1

Related Questions