Creating a hidden UISegmentedControl in a UITableV

2020-04-28 06:41发布

问题:

Like the iBooks app, when you pull down the tableview, a search bar and segmented control appear, to allow you to search and switch between two types of views.

It sticks in that position when you pull down far enough, and alternatively, gets hidden when you pull the tableview up enough.

I am trying to implement the same thing with a UISegmentedControl. So far I have added a segmented control successfully as a subview to the table. (It has a negative Y frame so make it stick above the tableview).

I have also implemented this code:

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    float yOffset = scrollView.contentOffset.y;

    if (yOffset < -70) {
        [scrollView setContentOffset:CGPointMake(0.0f, -70.0f) animated:YES];
    } else if (yOffset > -10) {
        [scrollView setContentOffset:CGPointMake(0.0f, -11.0f) animated:YES];
    }
}

This works great, until I try using the segmented control. Where the table will just act like it is scrolling, ignoring the segmented control altogether (i.e. if I tap on a segment, it doesn't even get selected, instead the table scrolls up, hiding the segmented control.

I did use the scrollViewDidScroll method but this made it buggy and the scrolling jumpy.

I also tried to make the segmented control's exclusiveTouch = YES, but this had no effect whatsoever.

I would be thankful for all help! thanks in advance!

回答1:

Here is my code which works:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //
    //  Table view
    //
    if ([scrollView isKindOfClass:[myTableView class]]) {
        //
        //  Discover top
        //
        CGFloat topY = scrollView.contentOffset.y + scrollView.contentInset.top;

        if (topY <= self.tableHeaderHeightConstraint.constant) {
            [self setIsScrolledToTop:YES];
        } else {
            [self setIsScrolledToTop:NO];
        }
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //
    //  Table view
    //
    if ([scrollView isKindOfClass:[myTableView class]]) {
        //
        //  Toggle favourite category
        //
        if ([self isScrolledToTop]) {
            //
            //  Show
            //
        } else {
            //
            //  Hide
            //
        }
    }
}

Edited the above code to make it a bit more generic, but syntactically its correct