Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Stretching / Resizing UITableView Header As The User Drags Down?

Using storyboard, I have placed an imageView as my tableView's headerView inside a ViewController.

This is how my storyboard is set up:

enter image description here

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:

enter image description here

But this is what I'm going for:

enter image description here

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!

like image 430
KingPolygon Avatar asked Jun 11 '13 21:06

KingPolygon


2 Answers

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

like image 139
petehare Avatar answered Sep 23 '22 15:09

petehare


First of all you should remove the UIImageView from the header and add it as a simple UIImageView on top of the UITableView then since UITableViewDelegate protocol conforms to UIScrollViewDelegate protocol you can implement the scrollViewDidScroll: method to check when the tableView is scrolling down and has a bouncing effect. something like this:

-(void)someInitMethod {    initialFrame = yourHeaderView.frame; } -(void)scrollViewDidScroll:(UIScrollView*)scrollView {     if(scrollView.contentOffset.y < 0) {        initialFrame.size.height -= scrollView.contentOffset.y;        yourHeaderView.frame = initialFrame;     } } 

Also make sure you set the proper contentMode for your UIImageView. 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.

like image 23
danypata Avatar answered Sep 23 '22 15:09

danypata