2

I am very new to Auto Layout and generally iOS development. I have been struggling with the Top Space of my ScrollView for quite a while and I cannot figure out what causes it.

enter image description here

The red container is the ScrollView and the grey container is a view encapsulating several views (label, picker view and text field).

The desired result is that the grey container starts totally from the top.

This is how my layout and constraints look like:

enter image description here

Any ideas will be much appreciated!

Adam
  • 1,054
  • 1
  • 12
  • 26
  • Are you missing a top constraint on your view inside the scroll view?. Try giving it constraint of 0. Also, I see errors/warnings in your constraints (see orange lines, and the red arrow next to your View Controller. Try fixing those and see if it works. – Gurtej Singh Aug 01 '15 at 19:32
  • I have a vertical space 0 on the grey container...The only warning is that the scrollview has ambitious height... – Adam Aug 01 '15 at 21:11
  • Difficult to say what is going on from just the screenshots, are you by any chance setting the content inset for your scroll view anywhere inside your code? – Gurtej Singh Aug 02 '15 at 06:46
  • No, I do not have any content inset - UIEdgeInsetsZero. Here are also some details about scrollview: frame = (16 64; 568 482); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = ; layer = ; contentOffset: {0, 0}; contentSize: {568, 482}> – Adam Aug 02 '15 at 07:49
  • Hmm. I'm out of ideas here. Will have to take a look at the complete code and find out what's going on. Is there a sample project you can share? – Gurtej Singh Aug 02 '15 at 08:11
  • Sure, there you go: [link](https://github.com/vongrad/iOSTranslations/tree/master/CoreDataApp) – Adam Aug 02 '15 at 08:27
  • You can download it as a zip in the right bottom corner. – Adam Aug 02 '15 at 08:45
  • Sorry, had already figured that out. I deleted my comment. Thanks anyways! – Gurtej Singh Aug 02 '15 at 08:45
  • I was able to resolve your issue, see my answer below. Let me know if you know a good place for me to upload the files for you if you need them. Cheers. – Gurtej Singh Aug 02 '15 at 09:06

1 Answers1

0

Ok, so your scroll view warning for ambiguous content height was causing the issue.

I was able to resolve your issue by adding your scroll view inside a another view and adding missing constraints. For reference: UIScrollView Scrollable Content Size Ambiguity.

My changes did make some issues to your margins etc, but you can fix them based on your need. Please let me know if this resolves your issue.

Screenshots for your reference:

enter image description here enter image description here

EDIT: Added screenshot for constraints of the wrapper view as well.

enter image description here

Community
  • 1
  • 1
Gurtej Singh
  • 3,244
  • 1
  • 14
  • 27
  • Did not you do it the other way though? In the stackoverflow post you linked here, they say that there should be a content view inside scroll view, however in you example it seems like you wrapped the scrollview in another view?? – Adam Aug 02 '15 at 09:30
  • Actually you are correct, I misread it :). Sorry about that. But, adding it inside a UIView did solve the issue for me. I edited my answer. Good catch :) – Gurtej Singh Aug 02 '15 at 09:32
  • Hope you feel that this can be an accepted answer. Let me know. Cheers – Gurtej Singh Aug 02 '15 at 09:40
  • Can you let me know what constraints have you added to the wrapper view? I does not seems to be working in my case... – Adam Aug 02 '15 at 13:12
  • Sure, I have added the screenshot for the wrapper view as well. Ignore the trailing space of -18, I have not spent much time on it. Notice the y as 64. Hope this solves your question. Thanks. – Gurtej Singh Aug 02 '15 at 15:10
  • Well, did it solve your issue? Can you please accept as the correct answer in case it did? Let me know. Thanks. – Gurtej Singh Aug 03 '15 at 05:08
  • It seems to be a workaround, however I guess it is not the idea solution for my problem. Therefore I will keep my question opened for one or two more days and see if somebody responds. Otherwise I will just accept yours. Hope thats all right. – Adam Aug 03 '15 at 15:00
  • Absolutely buddy :) even I would like to see a better solution here. Thanks for replying back :) – Gurtej Singh Aug 03 '15 at 15:47