Search code examples
iosswift

"Scrollable content size is ambiguous to "ScrollView"


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!


Solution

  • Here is the trick that worked for me:

    1. Add a ScrollView to your wished ViewController.

    2. Select it in the Outline and open its size inspector.

    3. Uncheck there the option "Content Layout Guides".

    4. Now set leading/top/trailing and bottom constrains to 0 of the ScrollView.

    5. Add in a UIView and constrain its leading/top/trailing and bottom also to 0.

    6. 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).

    7. 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.