I am trying to replicate something similar to Android's collapsing toolbar (see this link to understand the effect I am trying to achieve). I found a nice turnaround here.
My main UIScrollView
is named fgScrollView
and the secondary which is pinned on top is named bgScrollView
. The whole UIViewController
is embedded in a UINavigationController
.
I pin the fgScrollView
to the Container View. I pin the bgScrollView
top, right and left to the fgScrollView
and I give it a steady height of 220, and I place inside the bgScrollView
a UIImageView
which I also pin to all sides and I give it a height of 220. (All pins are with 0 distance)
This is how it looks from the IB (the UILabel
that you see in the hierarchy is placed there to give a scrollable height to the fgScrollView
):
In the scrollViewDidScroll
method I set:
bgScrollView.contentOffset.y = scrollView.contentOffset.y * 1.4
(I have already set only the fgScrollView to be the delegate and I chose the 1.4 randomly) and in Interface Builder I choose the Adjust Scroll View Insets option.
The result is this in the beginning:
but as soon as I click to scroll up this happens (you can see the gap - it stops being pinned to the top):
From thereon it stays like that when I scroll to the top.
I uncheck the Adjust Scroll View Insets option and a strange thing that I notice is that the image does not go under the navigation bar, although I have checked the options Under Top and Bottom Bars:
And when I start scrolling this happens:
and it stays like that even when I scroll back to the top.
I tried to fix the issue programmatically. I add the navigation bar and status bar heights and I declare this:
fgScrollView.contentInset = UIEdgeInsets(top: CGRectGetHeight((self.navigationController?.navigationBar.frame)!) + CGRectGetHeight(UIApplication.sharedApplication().statusBarFrame), left: 0,
bottom: CGRectGetHeight((self.navigationController?.toolbar.frame)!), right: 0)
and the result is this:
Any clues?...