Using storyboard, I have placed an imageView
as my tableView's
headerView
inside a ViewController
.
This is how my storyboard is set up:
Depending on what data the user is viewing, the viewController
will either show or hide the headerView
. My question is, that when the headerView
is visible and the user drags down on the tableView
, how can I have the imageView
stick to both the navigationBar
and the tableView
as it resizes to cover the space in between?
This is what it currently does:
But this is what I'm going for:
Any help would be greatly appreciated. I've looked at parallax libraries, but none support sectionTitles, and I'm not necessarily going for the parallax effect either. When the user scrolls up, I want it to bounce back to the regularView and not hide the headerView. Thanks!
UPDATE:
I have followed the advice posted by Dany below and have done the following:
-(void)scrollViewDidScroll:(UIScrollView*)scrollView { CGRect initialFrame = CGRectMake(0, 0, 320, 160); if (scrollView.contentOffset.y < 0) { initialFrame.size.height =! scrollView.contentOffset.y; childHeaderView.frame = initialFrame; } }
childHeaderView is an imageView and for some reason when I drag down, the image moves up (like half of it behind the navBar) and doesn't return. Any advice would be greatly appreciated!! Thanks!
First of all you should remove the
UIImageView
from the header and add it as a simpleUIImageView
on top of theUITableView
then sinceUITableViewDelegate
protocol conforms toUIScrollViewDelegate
protocol you can implement thescrollViewDidScroll:
method to check when thetableView
is scrolling down and has abouncing
effect. something like this:Also make sure you set the proper
contentMode
for yourUIImageView
. Also I think this implementation will create a bouncing effect but I'm not sure because I can't test it right now but I think this is a good start point for you.This is how I achieved it, in my case I was using a map view up the top:
viewDidLoad
add the following:tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0)
where200
is the height of the View. This will push the contents of the table view downwards.In the view controller add the following code, which resizes the view based on the scrolling:
If I could set
contentInset
from the storyboard it would be even more prettyPlease have a look at this https://github.com/matteogobbi/MGSpotyViewController which implements the same effect as per your requirement.
I don't know, if this would help you or not ..
Set your scroll delegate to self.
and then implement this:
I recently posted a blog post about accomplishing this using constraints which might help, turns out it was quite straight forward.
Here is the link: Creating parallax effect on UIScrollView using constraints
The earlier solutions on this page gave me some trouble when I needed this to work along with section titles and index bar, so I came up with the following alternative myself. Please note; I don't use autolayout in my project and I've only tested this on iOS9+;
In your project's storyboard:
Next, go to your UIViewController.m:
I needed this implementation only for a stretching header with background color and labels. It should be easy to add a UIImageView to this header though.
Also, steps 1 to 5 are completely optional of course. You can programmatically create your header view or use a XIB instead. As long as you make sure the table has a Clear Colored header view set with the same height as your desired header because this serves as a spacer to keep your cells and section titles in line.
EDIT:
I found an even cleaner way to accomplish this;
viewDidLoad
code above, there is no need to pull the UIView out of it's container and we won't need to set it as a table background.scrollViewDidScroll:
method to this:UIViewController.m:
That's it. Only visual difference from the other solution is that the contents will now scroll up along with the rest of the cells instead of being overlapped by them.