Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Wednesday, February 10, 2016

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

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!

Answer by danypata for iOS: Stretching / Resizing UITableView Header As The User Drags Down?


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.

Answer by Islam Adel for iOS: Stretching / Resizing UITableView Header As The User Drags Down?


I don't know, if this would help you or not ..

Set your scroll delegate to self.

and then implement this:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView  {      float scrollViewHeight = scrollView.frame.size.height;      float scrollContentSizeHeight = scrollView.contentSize.height;      float scrollOffset = scrollView.contentOffset.y;        if (scrollOffset == 0)      {          // then we are at the top      }      else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)      {          // then we are at the end          // Do what you need here       }  }  

Answer by petehare for iOS: Stretching / Resizing UITableView Header As The User Drags Down?


I recently posted a blog post about accomplishing this using constraints which might help, turns out it was quite straight forward.

http://blog.domesticcat.com.au/ios/2014/03/19/creating-parallax-effect-on-uiscrollview-using-simple-constraints/

Answer by Prabhu for iOS: Stretching / Resizing UITableView Header As The User Drags Down?


Please have a look at this https://github.com/matteogobbi/MGSpotyViewController which implements the same effect as per your requirement.

Answer by geometrikal for iOS: Stretching / Resizing UITableView Header As The User Drags Down?


This is how I achieved it, in my case I was using a map view up the top:

  1. Create a View Controller in storyboard.
  2. Add a Table View and set the constraints to 0 from all sides.
  3. Add a Map View (or whatever view) below the Table View so that it will get rendered over the top. It will look like it is overlapping.
  4. Add constraints to the top left and right.
  5. In the view controller viewDidLoad add the following: tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0) where 200 is the height of the View. This will push the contents of the table view downwards.
  6. In the view controller add the following code, which resizes the view based on the scrolling:

    func scrollViewDidScroll(scrollView: UIScrollView) {      var scrollOffset = scrollView.contentOffset.y      var headerFrame = mapView.frame      if (scrollOffset < 0) {          // Adjust map          headerFrame = CGRect(x: mapView.frame.origin.x,              y: mapView.frame.origin.y,              width: mapView.frame.size.width,              height: -scrollOffset)      } else {          // Adjust map          headerFrame = CGRect(x: mapView.frame.origin.x,              y: mapView.frame.origin.y,              width: mapView.frame.size.width,              height: 0)      }      mapView.frame = headerFrame  }  

If I could set contentInset from the storyboard it would be even more pretty


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.