diff --git a/TLYShyNavBar/ShyControllers/TLYShyScrollViewController.h b/TLYShyNavBar/ShyControllers/TLYShyScrollViewController.h index ca3e522..dc3b82f 100644 --- a/TLYShyNavBar/ShyControllers/TLYShyScrollViewController.h +++ b/TLYShyNavBar/ShyControllers/TLYShyScrollViewController.h @@ -17,6 +17,6 @@ @property (nonatomic, weak) UIScrollView *scrollView; @property (nonatomic, weak) TLYShyViewController *parent; -- (BOOL)updateLayoutIfNeeded:(BOOL)intelligently; +- (CGFloat)updateLayoutIfNeeded:(BOOL)intelligently; @end diff --git a/TLYShyNavBar/ShyControllers/TLYShyScrollViewController.m b/TLYShyNavBar/ShyControllers/TLYShyScrollViewController.m index 01531da..498bc5c 100644 --- a/TLYShyNavBar/ShyControllers/TLYShyScrollViewController.m +++ b/TLYShyNavBar/ShyControllers/TLYShyScrollViewController.m @@ -10,12 +10,6 @@ #import "../Categories/UIScrollView+Helpers.h" -@interface TLYShyScrollViewController () - -@property (nonatomic, assign) UIEdgeInsets previousScrollInsets; - -@end - @implementation TLYShyScrollViewController - (void)offsetCenterBy:(CGPoint)deltaPoint @@ -23,19 +17,28 @@ [self updateLayoutIfNeeded:NO]; } -- (BOOL)updateLayoutIfNeeded:(BOOL)intelligently +- (CGFloat)updateLayoutIfNeeded:(BOOL)intelligently { + if (self.scrollView.contentSize.height < FLT_EPSILON + && ([self.scrollView isKindOfClass:[UITableView class]] + || [self.scrollView isKindOfClass:[UICollectionView class]]) + ) + { + return 0.f; + } + CGFloat parentMaxY = [self.parent maxYRelativeToView:self.scrollView.superview]; CGFloat normalizedY = parentMaxY - self.scrollView.frame.origin.y; UIEdgeInsets insets = self.scrollView.contentInset; insets.top = normalizedY; - if (normalizedY > 0 && !UIEdgeInsetsEqualToEdgeInsets(insets, self.previousScrollInsets)) + if (normalizedY > -FLT_EPSILON && !UIEdgeInsetsEqualToEdgeInsets(insets, self.scrollView.contentInset)) { + CGFloat delta = insets.top - self.scrollView.contentInset.top; + if (intelligently) { [self.scrollView tly_smartSetInsets:insets]; - self.previousScrollInsets = insets; } else { @@ -43,7 +46,7 @@ self.scrollView.scrollIndicatorInsets = insets; } - return true; + return delta; } if (normalizedY < 0) @@ -52,10 +55,10 @@ frame = UIEdgeInsetsInsetRect(frame, insets); self.scrollView.frame = frame; - return true; + return [self updateLayoutIfNeeded:YES]; } - return false; + return 0.f; } @end diff --git a/TLYShyNavBar/ShyControllers/TLYShyViewController.m b/TLYShyNavBar/ShyControllers/TLYShyViewController.m index d07ddc2..0742836 100644 --- a/TLYShyNavBar/ShyControllers/TLYShyViewController.m +++ b/TLYShyNavBar/ShyControllers/TLYShyViewController.m @@ -234,6 +234,8 @@ { CGFloat amountToMove = self.contractedCenterValue.y - self.view.center.y; + [self _onAlphaUpdate:FLT_EPSILON]; + [self _updateCenter:self.contractedCenterValue]; [self.subShyController contract]; diff --git a/TLYShyNavBar/TLYShyNavBarManager.m b/TLYShyNavBar/TLYShyNavBarManager.m index ab27dc9..0fd3838 100644 --- a/TLYShyNavBar/TLYShyNavBarManager.m +++ b/TLYShyNavBar/TLYShyNavBarManager.m @@ -121,9 +121,17 @@ static void * const kTLYShyNavBarManagerKVOContext = (void*)&kTLYShyNavBarManage { _viewController = viewController; - UIView *navbar = viewController.navigationController.navigationBar; - NSAssert(navbar != nil, @"You are using the component wrong... Please see the README file."); + if ([viewController isKindOfClass:[UITableViewController class]] + || [viewController.view isKindOfClass:[UITableViewController class]]) + { + NSLog(@"*** WARNING: Please consider using a UIViewController with a UITableView as a subview ***"); + } + UIView *navbar = viewController.navigationController.navigationBar; + NSAssert(navbar != nil, @"Please make sure the viewController is already attached to a navigation controller."); + + viewController.extendedLayoutIncludesOpaqueBars = YES; + [self.extensionViewContainer removeFromSuperview]; [self.viewController.view addSubview:self.extensionViewContainer]; @@ -269,17 +277,16 @@ static void * const kTLYShyNavBarManagerKVOContext = (void*)&kTLYShyNavBarManage deltaY = MIN(0, availableResistance + deltaY); } // 5.2 - Only apply resistance if expanding above the status bar -#warning - TODO -// else if (self.scrollView.contentOffset.y > -[self.statusBarController calculateTotalHeightRecursively]) -// { -// CGFloat availableResistance = self.expansionResistance - self.resistanceConsumed; -// self.resistanceConsumed = MIN(self.expansionResistance, self.resistanceConsumed + deltaY); -// -// deltaY = MAX(0, deltaY - availableResistance); -// } + else if (self.scrollView.contentOffset.y > -[self.statusBarController calculateTotalHeightRecursively]) + { + CGFloat availableResistance = self.expansionResistance - self.resistanceConsumed; + self.resistanceConsumed = MIN(self.expansionResistance, self.resistanceConsumed + deltaY); + + deltaY = MAX(0, deltaY - availableResistance); + } // 6 - Update the navigation bar shyViewController - self.navBarController.fadeBehavior = (TLYShyNavBarFade)self.fadeBehavior; + self.navBarController.fadeBehavior = self.fadeBehavior; [self.navBarController updateYOffset:deltaY]; @@ -296,16 +303,7 @@ static void * const kTLYShyNavBarManagerKVOContext = (void*)&kTLYShyNavBarManage } self.resistanceConsumed = 0; - - CGFloat deltaY = [self.navBarController snap:self.contracting]; - CGPoint newContentOffset = self.scrollView.contentOffset; - - newContentOffset.y -= deltaY; - - [UIView animateWithDuration:0.2 - animations:^{ - self.scrollView.contentOffset = newContentOffset; - }]; + [self.navBarController snap:self.contracting]; } #pragma mark - KVO @@ -362,7 +360,7 @@ static void * const kTLYShyNavBarManagerKVOContext = (void*)&kTLYShyNavBarManage - (void)layoutViews { - if ([self.scrollViewController updateLayoutIfNeeded:YES]) + if (fabs([self.scrollViewController updateLayoutIfNeeded:YES]) > FLT_EPSILON) { [self.navBarController expand]; [self.extensionViewContainer.superview bringSubviewToFront:self.extensionViewContainer];