UIScrollView title view?

I have a controller that has a UIView on top with some buttons and labels. Underneath that, I have a UIScrollView that also contains content. I want the UIView on top to essentially work and display as the title of this scroll, similar to UITableViewHeader. I'm using the scrollViewDidScroll delegate method with contentOffset to try and get it to work, but I can't seem to get the math right.

The idea is that as the scroll is scrolled, the "title" will scroll with it relative to the content. It can even scroll the screen. Then when you scroll down, it will stick to the top of the scroll. As I said, very similar functionality to UITableViewHeader.

Any help calculating the math for this would be greatly appreciated!

Here's what I have so far:

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGRect updatedSegmentFrame = self.segmentedControlContainerView.bounds;
    CGRect updatedTableViewFrame = self.viewControllerThree.tableView.frame;

    CGFloat topEdge = -scrollView.contentOffset.y;

    if (topEdge >= 0) {
        updatedSegmentFrame.origin.y = 0;
    }
    else {
        updatedSegmentFrame.origin.y = -scrollView.contentOffset.y;
    }

    self.viewControllerThree.tableView.frame = updatedTableViewFrame;
    self.segmentedControlContainerView.frame = updatedSegmentFrame;
}

      

This code moves the "title view" appearance along with the UIScrollView with everything working as expected. The only thing missing is that when scrolling up, the UIScrollView will need to increase in size to offset the title of the View from the screen.

+3


source to share


1 answer


Here is the solution I presented for a header that behaves similarly to what you describe: FixedHeaderTableView .



In your case, you must pass 0 for the visibleHeaderHeight parameter.

+1


source







All Articles