ChatGPT解决这个技术问题 Extra ChatGPT

How to fix "Scrollable Content Size Ambiguity" in Xcode 11 (iOS 12, iOS 13) using Auto Layout

When using Auto Layout, I am unable to set up a simple UIScrollView in my view controller in Xcode 11 beta. I know that I must constrain the scroll view to the edges, and then set the scroll view width and height equal to the width and height of the entire view that contains the scroll view. However, I am not getting the option to set equal widths and heights when I attempt to do so.

When I do the right-click-drag from the scroll view to the entire main view, I get the following options:

Leading Space to Safe Area

Top Space to Safe Area

Trailing Space to Safe Area

Bottom Space to Safe Area

Center Horizontally in Safe Area

Center Vertically in Safe Area

In other videos, there is an "Equal Widths" and "Equal Heights" option that I don't seem to have.

Am I doing something wrong, or did Apple change the way scroll views work in Xcode 11?


J
Juan David Torres

Disabling the content layout guides in the size inspector (ruler icon) in properties

https://i.stack.imgur.com/iNUax.png

I was having the same issue, and by disabling the option it was gone.

Hope it helps! :)


I had figured out another way to make it work, but this is much simpler. Thank you!
@PranavRamesh I am using autoresize and the content layout guide option is already disabled but I have the same issue. What is your other solution ?
@PranavRamesh What is your other solution?
Although removing the Content Layout Guide, solve this warning, I believe the right way to fix it is updating the constraints to Content Layout Guide or Frame Layout Guide, when possible. In my case, I used Frame Layout to leading, trailing, top and bottom; and Content Layout to width and height and solve my problem.
@Shehroz Xcode would automatically constraint the content view's sides to the scroll view frame layout. Simply changing from constraining to frame layout to superview worked. When you make your constraints in the interface builder, you would need to go through all four constraints and switch from frame layout to superview.
V
Vadim

I stacked with that problem as well. Found a good guide that helped me:

https://useyourloaf.com/blog/scroll-view-layouts-with-interface-builder/

Basically what you need is 9 constraints (assuming you want to scroll only vertically):

1-4: ScrollView to Superview (top, bottom, leading, trailing). Make sure to connect it to parent view and not to safe area.

5-8: Content view to Content Layout guide (top, bottom, leading, trailing).

Content view Width equals width to Frame Layout Guide.


For 1-4 , using safe area also works and I think safe area should be used.
R
Romil Patel

I also encountered this problem (Version 11.0 beta 3 (11M362v)). I solved this problem by first setting the layout in xcode10 and then running it in xcode11. I haven't found any official instructions yet, proving that this is a problem with xcode11, but for now, I guess this is a problem with xcode11 bate.


A
Akshar Darji

After spending a long time on this scrolling issue in Xcode-11. The conclusion on this issue is you have to choose the scrolling option while adding a new constraint.

You can refer to the following screenshot to resolve your issue.

https://i.stack.imgur.com/CKNO1.png

These mentioned solutions worked for me.


I believe this is the right way to solve this warning. Works for me, thanks! In my case, I used Frame Layout to leading, trailing, top and bottom; and Content Layout to width and height.
D
Divesh singh

Disabling the content layout guides in properties worked for me.


m
mate00

https://i.stack.imgur.com/UFdYl.png

Embed Scroll view in a UIview.

give Top, Bottom, Right and Left 0.

now give constraints of scroll view with this view.

Add Equal height and width of content view to this View.

parent view is automatically giving Safe Area. All you have to do is just embed your scroll view in a UIview and then give equal height and equal width of the content view to this view.


R
RJE

@Vadim's answer worked with a little adjustment. Nothing else worked and I didn't want to disable 'Content layout guide'. Since apple enabled it, why disable it. So there must be something I'm missing or it's a bug.

Vadim's answer is:

I stacked with that problem as well. Found a good guide that helped me:
https://useyourloaf.com/blog/scroll-view-layouts-with-interface-builder/
Basically what you need is 9 constraints (assuming you want to scroll only vertically):

1-4: ScrollView to Superview (top, bottom, leading, trailing). Make sure to connect it to parent view and not to safe area.

5-8: Content view to Content Layout guide (top, bottom, leading, trailing)

9: Content view Width equals width to Frame Layout Guide.

All good, but it didn't work,

But then I realize that the above 6 and 8 (trailing and bottom constraints to 'Content Layout guide') constraints are a little weird. They had positive constant values instead of zero. Like the below image. (constant equals to width and height of the scroll view). I changed them to zero and now it all works.

https://i.stack.imgur.com/5gJCU.png


V
Vinoth Vino

For me, initially the Content Layout Guides is in disabled state by default in ScrollView. So I just enabled and disabled the Content Layout Guides then the error disappeared automatically.


A
Ahmed Abdallah

Xcode 11+, Swift 5.

I solved my issue, I prepared video and code