1

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:

2
  • so what have you done so far? can you show us the code? Commented Oct 28, 2016 at 19:56
  • I don't have something for the scrollview. I don't know what is better to put it from storyboard or code? Commented Oct 28, 2016 at 19:58

2 Answers 2

4

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)

Sign up to request clarification or add additional context in comments.

3 Comments

I generate textfields with this command tf.frame = CGRect(x: 50 ,y: self.ypos, width: 220, height: 30) self.scrollView.addSubview(tf) I also use a UiView but again it doesn't scroll
It's not enough to set the frames, you also have to set constraints. Another solution is to calculate the height of the scrollView and then set the scrollView's contentSize with the following: scrollView.contentSize = CGSize(width: 375, height: yourHeightValue)
it worked with scrollView.contentSize = CGSize(width: 375, height: yourHeightValue). Update your answer if you want and thank you!
1

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.

3 Comments

Hi thanks for the answer! First question can I skip height and width if I use leading trailing and vertical spacing?
yea height of scrollview is automatically adjusted by its contents so just provide constraint to each of its TextFields , it will work . Also make it correct answer if it solves your problem. Thanks
More than I can see

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.