Search code examples
iosobjective-cuiscrollviewoffsetstatusbar

Content of UIScrollView has offset below Navigation Bar


I have a UIScrollView which contains a UIView, that in turn has an UIImageView and several UIButtons in it. The problem I have is that the image is displayed about 20 px underneath my navigation bar.

People who seem to have similar problems had solutions like setting

self.automaticallyAdjustsScrollViewInsets = NO;

or just hide the status bar

- (BOOL)prefersStatusBarHidden {
    return YES;
}

However, both solutions don't work for me. The first one sets the contents too high and adds the 20px space at the bottom instead. The second one hides the status bar , but the offset still remains.

problem

Since I have a lot of the code from this tutorial by Ray Wenderlich and I have no idea where exactly in my code the problem lies, this is the link to code on github.

Can anyone help me?


Solution

  • I was able to figure out what is going on. Thanks for posting the link to your app.

    The issue is this following code:

    if (contentsFrame.size.height < boundsSize.height) {
        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
    } else {
        contentsFrame.origin.y = 0.0f;
    }
    

    It turns out the contentsFrame.size.height is in fact less than bounds.Size.height when the map is first displayed. So your code is centering the image vertically. That's why you see that offset at the top. 17.279 pixels to be exact.

    I've been playing around with your app (which seem very interesting btw) and I believe that you can get rid of the two calls to centerScrollViewContents altogether and it will work just as you expected.

    Let me know if you have more questions.

    Hope this helps!