Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrollview with navigation bar + auto layout

I have a ViewController embedded in NavigationController. I placed ScrollView inside it, then content view covering the full area (to set ScrollView's content size) and some labels. I tried to set constraints for all of the controls but I failed.

This is how it looks in Xcode:

Xcode view

And this is in Simulator:

Simulator view

Red color is scroll view, green – content view. Of course I can scroll the view to get the bottom label on screen.

What I did wrong? What are the proper constraints? I want of course the ScrollView to cover the entire usable area of window (from bottom of navigation bar to bottom of screen).

For full reference you can download the project.

like image 511
bpiec Avatar asked Jan 07 '16 19:01

bpiec


2 Answers

Swift 3:

self.automaticallyAdjustsScrollViewInsets = false
like image 63
Roshan lamichhane Avatar answered Sep 18 '22 14:09

Roshan lamichhane


I think the gap you see is caused by the navigation bar. The gap is off size 64 which is the size of the navigation bar + status bar (44+20). You need to constraint the scroll bar to the top layout guide.By pushing this 64 negatively. So your scrollview top constriant will be -64.

This can also be done in one click, by clicking on the position view button in size inspector in xcode.

Click on the position view and select fill container vertically.This will constraint the scrollview to top layout guide. Now select the scrollview and click on the update constraints in the storyboard.

enter image description here

As you can see, the contentView is not constrianed to cover the view just like scrollview , there is space at the button of the content view, when the view loads, the navigation bar will occupy the additional space, and the contentview will be exactly at where you need.

like image 23
Teja Nandamuri Avatar answered Sep 22 '22 14:09

Teja Nandamuri