bryankeller / BLKFlexibleHeightBar

Create condensing header bars like those seen in the Facebook, Square Cash, and Safari iOS apps.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I snap the progress to 1 right after my FlexibleHeightBar initialized?

parachvte opened this issue · comments

I have a search bar on top of a UIScrollView, and I'd like to hide the searchBar initially. And show the search bar when scrollView scrolls up, code like this:

- (void)viewDidLoad {
    ...
    searchBar = [[BLKFlexibleHeightBar alloc] initWithFrame:CGRectMake(0, 0, width, 64)];
    searchBar.minimumBarHeight = 0;
    searchBar.maximumBarHeight = 64;

    searchBar.backgroundColor = [UIColor colorWithRed:0.86 green:0.25 blue:0.23 alpha:1];
    searchBar.behaviorDefiner = [SquareCashStyleBehaviorDefiner new];
    [searchBar.behaviorDefiner addSnappingPositionProgress:0.0 forProgressRangeStart:0.0 end:0.5];
    [searchBar.behaviorDefiner addSnappingPositionProgress:1.0 forProgressRangeStart:0.5 end:1.0];

    _scrollView.delegate = (id<UITableViewDelegate>)searchBar.behaviorDefiner;
    [_scrollView addSubview:searchBar];

    [searchBar.behaviorDefiner snapToProgress:1.0 scrollView:_scrollView];
}

but a searchBar is appear in maximum height on the _scrollView... So I add an RACObserve on searchBar.process like:

[RACObserve(searchBar, progress) subscribeNext:^(id x) {
    NSLog(@"process %f", searchBar.progress);
}];

Logs:

process 0.000000
process 1.000000
process 1.000000
process 0.000000

The searchBar's process snapped to 1, the then confusedly snapped back to 0....

Just move [searchBar.behaviorDefiner snapToProgress:1.0 scrollView:_scrollView]; to viewWillAppear.

The problem is that scrollViewDidScroll delegate method gets called internally when gets draw by first time (so after viewDidLoad) to adjust the contentSize to the contentInset, and is there where this library logic is calculating the progress based on the last scrolled point, and since there isn't any yet, it resets it to 0. If you put a breakpoint in setProgress you will see it.

@palcalde Thanks. u are right. Since I want to make my search bar looks like a widget, it's unlikely to access to the search bar in viewWillAppear. Finally I moved snapToProgress:scrollView: to an dispatch block. It works, but Ugly :(

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_MSEC),
    dispatch_get_main_queue(), ^{
        [_searchBar.behaviorDefiner snapToProgress:1.0 scrollView:_scrollView];
    });
commented

Well I met another weird issue, my BLK bar stays at progress 1.0, but when switching tabs (UICollectionView Cells), the BLK bar will suddenly change back to progress 0. If I want to keep it at its current progress, any way to do it? It is better to check the state and not put it in a main queue to move it back.

I'm not sure why the bar resets to 0 on willAppear. Try to put a breakpoint on setProgress and see who calls it and why, I don't see anything in BLK library that changes the progress automatically, so should be somewhere in your code.

You can always use viewWillAppear and do [self.yourBLKBar.behavior snapToProgress:1.0 scrollView:self.tableView]

commented

#31, this is my issue and I have know idea why.