1

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!

Adam Carter
  • 4,741
  • 5
  • 42
  • 103
  • 1
    Are you using a search bar as well? In that case you can use the `scopeButtonTitles` / `showsScopeBar` properties of it instead, which has this behavior built-in, and is the way iBooks (and countless other apps) does it. – Hampus Nilsson Aug 15 '12 at 19:52
  • No, just a segmented control. I have updated the code from `yOffset < -70` to `yOffset <= -70` to make the scrolling stop happening when I tap, however, when clicking on the segments, they are not updating to the selected (tapped) segment. – Adam Carter Aug 15 '12 at 19:54

1 Answers1

0

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

Adam Carter
  • 4,741
  • 5
  • 42
  • 103