bryankeller/BLKFlexibleHeightBar

How to pin a subview in BLKFlexibleHeightBar

liuxuan30 opened this issue · 1 comments

I am trying to modify the demo FacebookStyleBar.m:

I am looking for that when user scrolls down, the whiteView keeps the height, just moves up along with the blueBarView, not shrinking, and when the blueBarView is out of screen, the whiteView will stay pinned to top screen,

I am trying to modify finalWhiteBarLayoutAttributes.transform to below, which I definitely misunderstood:

    BLKFlexibleHeightBarSubviewLayoutAttributes *finalWhiteBarLayoutAttributes = [[BLKFlexibleHeightBarSubviewLayoutAttributes alloc] init];
    finalBlueBarLayoutAttributes.frame = CGRectMake(0, 20, self.frame.size.width, 40);
    [whiteBarView addLayoutAttributes:finalWhiteBarLayoutAttributes forProgress:1];

the animation is totally messed up while scrolling.

I tried to use follow the demo code

    BLKFlexibleHeightBarSubviewLayoutAttributes *finalWhiteBarLayoutAttributes = [[BLKFlexibleHeightBarSubviewLayoutAttributes alloc] initWithExistingLayoutAttributes:initialWhiteBarLayoutAttributes];
    finalWhiteBarLayoutAttributes.transform = CGAffineTransformMakeTranslation(0, -(105-20-40));
    [whiteBarView addLayoutAttributes:finalWhiteBarLayoutAttributes forProgress:1];

but still performs as the old style.

Could you take a look where am I wrong? Just want the whiteView keep it's size and moves up to the top margin

Spend lots of time on it, and finally I write a custrom bahavior definer to do my job.

Some key notes are:

  1. make sure you set your scroll view's contentInset
  2. setup min height equals the pinned view height

Behaviour definer is just the same as the facebook style code. nothing more.

View controller demo code:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNeedsStatusBarAppearanceUpdate];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor blueColor];
    BLKFlexibleHeightBar *myBar = [[BLKFlexibleHeightBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100)];
    myBar.minimumBarHeight = 40;
    myBar.backgroundColor = [UIColor redColor];
    [self.view addSubview:myBar];
    myBar.behaviorDefiner = [[MyBehaviorDefiner alloc] init];
    UIView *menu = [[UIView alloc] init];
    self.collectionView.delegate = myBar.behaviorDefiner;
    self.collectionView.contentInset = UIEdgeInsetsMake(myBar.maximumBarHeight, 0, 0, 0); // must have
    BLKFlexibleHeightBarSubviewLayoutAttributes *initialViewLayoutAttributes = [[BLKFlexibleHeightBarSubviewLayoutAttributes alloc] init];
    initialViewLayoutAttributes.frame = CGRectMake(0,60,self.view.bounds.size.width,40);
    [menu addLayoutAttributes:initialViewLayoutAttributes forProgress:0];
    BLKFlexibleHeightBarSubviewLayoutAttributes *finalViewLayoutAttributes = [[BLKFlexibleHeightBarSubviewLayoutAttributes alloc] initWithExistingLayoutAttributes:initialViewLayoutAttributes];
    finalViewLayoutAttributes.transform = CGAffineTransformMakeTranslation(0, -40);
    [menu addLayoutAttributes:finalViewLayoutAttributes forProgress:1.0];
    menu.backgroundColor = [UIColor yellowColor];
    [myBar addSubview:menu];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 20;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
    return cell;
}