Reputation: 41
I am a little stuck with the ScrollView in Swift and Xcode 11.
My goal is a simple ScrollView with Labels and Textfields. Each label describes what the textfields are used for. For example there is a Label "Name", so you have to enter your name in the textfield under the label. For this I need a ScrollView because the App I am coding requires a few more information. When I am trying to put this in a ScrollView I am constantly getting the following error: "Scrollable content size is ambiguous to ScrollView".
In order to find help I searched the internet and for example found this question: UIScrollView Scrollable Content Size Ambiguity. Unfortunatly it didn't solve my problem. So I decided to create a small test project with the following: - I created a project as a Single View Application - I added a ScrollView to the ViewController - I clicked on the constrains-Button at the canvas - I selected 0,0,0,0 for leading/top/trailing/bottom. - I clicked on "add 4 Constrains - Now the warning appeared. Also the ScrollView was not resized to the ViewController... I don't know why. - I added a UIView inside the ScrollView and set the same constrains, this time according to the ScrollView. - The UIView (Content view) got an extra constrain: Equal width with the root view - Now I added two Buttons, one with the text "Hello" and the other one with "world" on it. - The "hello"-Button received the following constrains: 16 to leading, trailing and top, as well as a fixed height of 30. - The "world"-Button got the following constrains: 16 to bottom, leading, trailing and over a 1000 so you can scroll through the ScrollView.
After all these steps the error is still there and if I run the app the "hello"-Button is at the top of the screen and the "world"-Button is at the bottom and I can't scroll.
Can maybe anyone help me fixing this issue. I am looking forward to every answer! Thank you for your help in advance!
Upvotes: 1
Views: 5938
Reputation: 1
If you add the scrollview and give 4 corner constraint then it will give the ambiguity error. here are steps how we fix this issue.
Step 1: Add ScrollView to the ViewController XIB or storyboard Add constraints to all four corners.
Step 2: Add a UIView inside the UIScrollview and add constraints with the scrollView to all four corners.
Step 3: Set equal width between the UIView and the scrollView. Add height to the UIView (If the height is greater than the height of the device, It automatically becomes scrollable). Now view inside scrollview have 6 constraints.
Step 4: Add the other UI whatever you want inside uiView.
Here are live demo watch: https://www.youtube.com/watch?v=nvNjBGZDf80
Upvotes: -1
Reputation: 891
Add a total of 9 constraints
1 - 4: ScrollView to Superview (leading, top, trailing & bottom)
5 - 8: Content view to Content Layout guide (leading, top, trailing & bottom)
9: Content view Width equals width to Frame Layout Guide.
But when you do this (right-click drag and drop) 7 & 8 constrains (trailing & bottom constrains to Content Layout guide) will have constant values. (check image below). Just make them zero. Now it worked.
Upvotes: 0
Reputation: 41
Here is the trick that worked for me:
Add a ScrollView to your wished ViewController.
Select it in the Outline and open its size inspector.
Uncheck there the option "Content Layout Guides".
Now set leading/top/trailing and bottom constrains to 0 of the ScrollView.
Add in a UIView and constrain its leading/top/trailing and bottom also to 0.
Add an equal width constrain to the UIView. (The width needs to be equal the width of the view from the ViewController, with this way you are disabeling horizontal scrolling).
The warning will disappear if every element inside the UIView is chained vertically. This means, that the top element has a constrain to the top of the view and to the element under it and so on. The last element needs a constrain to the bottom of the view.
If you followed this steps you should be fine with ScrollViews. This way you also can add as many content as you want to the bottom and the ScrollView will extend dynamically.
I hope I explained it well enough.
Upvotes: 3
Reputation: 116
It sounds like you're on the right track, and really close.
The first part is absolutely right - you add your ScrollView and pin it to zero for the top, trailing, bottom, and leading constraints. That makes it take up your whole screen.
Then you drop a plain old UIView into the ScrollView, and pin its top, trailing, bottom, and leading constraints to the ScrollView (all as zero again). Then you set the UIView to have an equal width to the ScrollView. The last thing is to set the UIView's height as equal to the ScrollView's height, but you change one thing: you set the priority of this constraint to be low (250). That's basically what allows the UIView to exceed the size of the ScrollView, so you can then scroll.
All you do then is add your buttons, etc inside the UIView, so you place them in relation to it rather than the ScrollView, and pin or align them as you wish.
Upvotes: 2