andreamazz / AMScrollingNavbar

Scrollable UINavigationBar that follows the scrolling of a UIScrollView
MIT License
6.05k stars 633 forks source link

showNavBarAnimated doesn't animate constraints. #119

Closed markst closed 9 years ago

markst commented 9 years ago

When calling hideNavbarAnimated: & showNavBarAnimated: combined with scrollable view constraints, the constraints are not animated properly.

The reason for this is the setNeedsLayout method within the animation block. The correct way to animate constraints is to call layoutIfNeeded within an animation block.

See "Animating Changes Made by Auto Layout": https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AutoLayoutbyExample/AutoLayoutbyExample.html

The following resolves animation issues for me:

- (void)showNavBarAnimated:(BOOL)animated
{
    if (self.scrollableView != nil) {
        BOOL isTracking = self.panGesture.state == UIGestureRecognizerStateBegan || self.panGesture.state == UIGestureRecognizerStateChanged;
        if (self.collapsed || isTracking) {
            self.panGesture.enabled = NO;
            if (!self.scrollableViewConstraint) {
                // Frame version
                CGRect rect = [self scrollView].frame;
                rect.origin.y = 0;
                [self scrollView].frame = rect;
            }
            [UIView animateWithDuration:animated ? 0.1 : 0 animations:^{
                self.scrollableHeaderConstraint.constant = 0;
                self.lastContentOffset = 0;
                self.delayDistance = -self.navbarHeight;
                [self scrollWithDelta:-self.navbarHeight];
                [self.view layoutIfNeeded];
            } completion:^(BOOL finished) {
                self.panGesture.enabled = YES;
            }];
        } else {
            [self updateNavbarAlpha:self.navbarHeight];
        }
    }
}
andreamazz commented 9 years ago

Hi @markst You're right, that was a brain fart on my part, I'll fix it right away. thanks for the heads up

markst commented 9 years ago

No worries. Thanks for creating this. I think layoutIfNeeded needs to be set on other animation blocks too.