Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make UIView scroll with UITableView but pin to top out of view

I currently have a view controller that is comprised of a Navigation bar, followed by a UIView that has two UIButtons added as subViews. There is then a UITableView underneath that begins at the bottom of the container UIView.

At the moment, when the user scrolls the UITableView it goes behind the UIView and UIButtons. What I actually want to happen is for the UIView and UIButtons to move up with the table view but only by the value of their height which in this case is 58 pixels. The flow would be like this...

1) Table scrolls and the UIView moves with it for the first 58 pixels.

2) The user continues to scroll the table but the UIView "pins" itself just out of view under the navigation bar.

3) When the user scrolls the table back down the UIView is then picked up and dragged back into view. I believe the new Facebook app does something similar in the timeline.

I don't want to set the UIView as the TableHeaderView of the table as I also have a pull-to-refresh which then sits above the buttons and looks terrible. I've tried playing around with the contentOffset properties of the underlying scrollview of the table but have hit a brick wall.

Any advice on where to start would be appreciated.

Thanks

EDIT: I am gotten a little further and using this code to move the frame of the UIView.

-(void) scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog (@"Content Offset: %f", self.tableView.contentOffset.y);
    NSLog (@"Button Frame: %f", self.btnBackground.frame.origin.y);

        if (self.tableView.contentOffset.y > 0)
        {
            CGRect newFrame = self.btnBackground.frame;
            newFrame.origin.x = 0;
            newFrame.origin.y = -self.tableView.contentOffset.y;
            [self.btnBackground setFrame: newFrame];
        }  

}

The problem now is that the scrollViewDidScroll delegate method doesn't get fired quickly enough if the table view is scrolled fast. The result is that the UIView doesn't quite make all way back to its original position when scroll quickly.

like image 348
Paul Morris Avatar asked Nov 13 '22 06:11

Paul Morris


1 Answers

The scroll content offset is a good idea. Also if you tableview has only one section one approach is to do a custom header view representing the top level widgets. If there is more than one sections create an additional empty section which would return your custom header.

You can refer to this stack overflow post. Customize UITableview Header Section

like image 94
bret Avatar answered Nov 15 '22 07:11

bret