Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

UIPageViewController support, scaling & new delegate methods #166

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion TLYShyNavBar/ShyControllers/TLYShyStatusBarController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static inline CGFloat AACStatusBarHeight(UIViewController *viewController)
}

// Modal views do not overlap the status bar, so no allowance need be made for it
if (viewController.presentingViewController != nil)
if (viewController.presentingViewController.presentedViewController == viewController)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This a better way to check if it's a modal. The current implementation fails in some cases (e.g. if a UITabBarController is pushed onto a UIViewController).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Coledunsby @telly Should be fixed in main repo! Thanks for your PR.

{
return 0.f;
}
Expand Down
7 changes: 7 additions & 0 deletions TLYShyNavBar/ShyControllers/TLYShyViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ typedef CGFloat(^TLYShyViewControllerContractionAmountBlock)(UIView *view);

@property (nonatomic) TLYShyNavBarFade fadeBehavior;

@property (nonatomic, readonly) BOOL contracted;
@property (nonatomic, readonly) BOOL expanded;

/* Scale means the navbar's subviews will scale as the navbar contracts/expands.
*/
@property (nonatomic) BOOL scale;

/* Sticky means it will always stay in expanded state
*/
@property (nonatomic) BOOL sticky;
Expand Down
57 changes: 46 additions & 11 deletions TLYShyNavBar/ShyControllers/TLYShyViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ - (CGFloat)maxYRelativeToView:(UIView *)superview

- (CGFloat)calculateTotalHeightRecursively
{
float overlap = [self.parent maxYRelativeToView:self.view];
if ([self.parent isMemberOfClass:[TLYShyStatusBarController class]] && overlap - self.view.frame.origin.y > 0) {
overlap += overlap - self.view.frame.origin.y;
}
return CGRectGetHeight(self.view.bounds) - overlap + [self.parent calculateTotalHeightRecursively];
return (self.expanded ? CGRectGetHeight(self.view.bounds) : 0) + [self.parent calculateTotalHeightRecursively];
}

@end
Expand All @@ -39,9 +35,6 @@ @interface TLYShyViewController ()

@property (nonatomic, assign) CGPoint contractedCenterValue;

@property (nonatomic, assign) BOOL contracted;
@property (nonatomic, assign) BOOL expanded;

@end

@implementation TLYShyViewController
Expand Down Expand Up @@ -81,6 +74,12 @@ - (BOOL)expanded

#pragma mark - Private methods

- (void)_onProgressUpdate:(CGFloat)progress
{
[self _onAlphaUpdate:progress];
[self _onScaleUpdate:progress];
}

- (void)_onAlphaUpdate:(CGFloat)alpha
{
if (self.sticky)
Expand Down Expand Up @@ -125,6 +124,32 @@ - (void)_updateSubviewsAlpha:(CGFloat)alpha
}
}

- (void)_onScaleUpdate:(CGFloat)scale
{
if (self.sticky)
{
[self _updateSubviewsScale:1.f];
return;
}

[self _updateSubviewsScale:self.scale ? scale : 1.f];
}

- (void)_updateSubviewsScale:(CGFloat)scale
{
if (![self.view isKindOfClass:[UINavigationBar class]])
{
return;
}

UINavigationBar *navigationBar = (UINavigationBar *)self.view;

for (UIView* view in navigationBar.topItem.titleView.subviews)
{
view.transform = scale < 1 ? CGAffineTransformMakeScale(scale, scale) : CGAffineTransformIdentity;
}
}

- (void)_updateCenter:(CGPoint)newCenter
{
CGPoint currentCenter = self.view.center;
Expand All @@ -146,6 +171,16 @@ - (void)setFadeBehavior:(TLYShyNavBarFade)fadeBehavior
}
}

- (void)setScale:(BOOL)scale
{
_scale = scale;

if (!scale)
{
[self _onScaleUpdate:1.f];
}
}

- (void)offsetCenterBy:(CGPoint)deltaPoint
{
self.view.center = CGPointMake(self.view.center.x + deltaPoint.x,
Expand Down Expand Up @@ -173,7 +208,7 @@ - (CGFloat)updateYOffset:(CGFloat)deltaY
CGFloat newAlpha = 1.f - (self.expandedCenterValue.y - self.view.center.y) / self.contractionAmountValue;
newAlpha = MIN(MAX(FLT_EPSILON, newAlpha), 1.f);

[self _onAlphaUpdate:newAlpha];
[self _onProgressUpdate:newAlpha];

residual = newYOffset - newYCenter;

Expand Down Expand Up @@ -236,7 +271,7 @@ - (CGFloat)expand
{
self.view.hidden = NO;

[self _onAlphaUpdate:1.f];
[self _onProgressUpdate:1.f];

CGFloat amountToMove = self.expandedCenterValue.y - self.view.center.y;

Expand All @@ -250,7 +285,7 @@ - (CGFloat)contract
{
CGFloat amountToMove = self.contractedCenterValue.y - self.view.center.y;

[self _onAlphaUpdate:FLT_EPSILON];
[self _onProgressUpdate:FLT_EPSILON];

[self _updateCenter:self.contractedCenterValue];
[self.subShyController contract];
Expand Down
17 changes: 15 additions & 2 deletions TLYShyNavBar/TLYShyNavBarManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
@property (nonatomic, readonly) CGRect extensionViewBounds;

/* Make the navigation bar stick to the top without collapsing
* Deatuls to NO
* Defaults to NO
*/
@property (nonatomic) BOOL stickyNavigationBar;

Expand All @@ -62,11 +62,20 @@
@property (nonatomic) CGFloat expansionResistance; // default 200
@property (nonatomic) CGFloat contractionResistance; // default 0

/* Check the state of the control.
*/
@property (nonatomic, readonly) BOOL contracting;

/* Choose how the navbar fades as it contracts/expands.
* Defaults to FadeSubviews
*/
@property (nonatomic) TLYShyNavBarFade fadeBehavior;

/* Choose if the navbar's subviews scale as the navbar contracts/expands.
* Defaults to NO
*/
@property (nonatomic) BOOL scale;

/* Use this to set if the controller have any kind of custom refresh control
*/
@property (nonatomic) BOOL hasCustomRefreshControl;
Expand All @@ -91,9 +100,13 @@

@optional

- (void)shyNavBarManagerDidBecomeFullyContracted:(TLYShyNavBarManager *) shyNavBarManager;
- (void)shyNavBarManagerDidStartContracting:(TLYShyNavBarManager *) shyNavBarManager;
- (void)shyNavBarManagerDidFinishContracting:(TLYShyNavBarManager *) shyNavBarManager;
- (void)shyNavBarManagerDidBecomeFullyContracted:(TLYShyNavBarManager *) shyNavBarManager;

- (void)shyNavBarManagerDidStartExpanding:(TLYShyNavBarManager *) shyNavBarManager;
- (void)shyNavBarManagerDidFinishExpanding:(TLYShyNavBarManager *) shyNavBarManager;
- (void)shyNavBarManagerDidBecomeFullyExpanded:(TLYShyNavBarManager *) shyNavBarManager;

@end

Expand Down
124 changes: 107 additions & 17 deletions TLYShyNavBar/TLYShyNavBarManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ @interface TLYShyNavBarManager () <UIScrollViewDelegate>
@property (nonatomic, assign) CGFloat previousYOffset;
@property (nonatomic, assign) CGFloat resistanceConsumed;

@property (nonatomic, assign) BOOL startedContracting;
@property (nonatomic, assign) BOOL startedExpanding;
@property (nonatomic, assign) BOOL fullyContracted;
@property (nonatomic, assign) BOOL fullyExpanded;

@property (nonatomic, assign) BOOL contracting;
@property (nonatomic, assign) BOOL previousContractionState;

@property (nonatomic, readonly) BOOL isViewControllerVisible;
@property (nonatomic, readonly) BOOL isPageViewController;

@end

Expand All @@ -58,13 +64,18 @@ - (instancetype)init
self.delegateProxy = [[TLYDelegateProxy alloc] initWithMiddleMan:self];

/* Initialize defaults */
self.startedContracting = NO;
self.startedExpanding = NO;
self.fullyContracted = NO;
self.fullyExpanded = YES;
self.contracting = NO;
self.previousContractionState = YES;

self.expansionResistance = 200.f;
self.contractionResistance = 0.f;

self.fadeBehavior = TLYShyNavBarFadeSubviews;
self.scale = NO;
self.previousYOffset = NAN;

/* Initialize shy controllers */
Expand All @@ -80,7 +91,7 @@ - (instancetype)init
self.extensionController.view = self.extensionViewContainer;

/* hierarchy setup */
/* StatusBar <-- navbar <-->> extension <--> scrollView
/* StatusBar <--> navbar <--> extension <--> scrollView
*/
self.navBarController.parent = self.statusBarController;
self.navBarController.child = self.extensionController;
Expand Down Expand Up @@ -167,10 +178,24 @@ - (void)setScrollView:(UIScrollView *)scrollView
self.delegateProxy.originalDelegate = _scrollView.delegate;
_scrollView.delegate = (id)self.delegateProxy;
}

if (self.isPageViewController)
{
UIEdgeInsets insets = UIEdgeInsetsMake(self.extensionController.calculateTotalHeightRecursively,
self.scrollView.contentInset.left,
self.scrollView.contentInset.bottom,
self.scrollView.contentInset.right);

[self.scrollView tly_setInsets:insets];

[self cleanup];
[self layoutViews];

self.previousYOffset = NAN;
}
else
{
[self cleanup];
[self layoutViews];
}

[_scrollView addObserver:self forKeyPath:@"contentSize" options:0 context:kTLYShyNavBarManagerKVOContext];
}

Expand All @@ -184,6 +209,11 @@ - (BOOL)isViewControllerVisible
return self.viewController.isViewLoaded && self.viewController.view.window;
}

- (BOOL)isPageViewController
{
return [self.viewController isKindOfClass:[UIPageViewController class]];
}

- (void)setDisable:(BOOL)disable
{
if (disable == _disable)
Expand Down Expand Up @@ -230,6 +260,66 @@ - (void)setStickyExtensionView:(BOOL)stickyExtensionView
self.extensionController.sticky = stickyExtensionView;
}

- (void)setStartedContracting:(BOOL)startedContracting
{
if (_startedContracting == startedContracting)
{
return;
}

_startedContracting = startedContracting;

if (startedContracting && [self.delegate respondsToSelector:@selector(shyNavBarManagerDidStartContracting:)])
{
[self.delegate shyNavBarManagerDidStartContracting:self];
}
}

- (void)setStartedExpanding:(BOOL)startedExpanding
{
if (_startedExpanding == startedExpanding)
{
return;
}

_startedExpanding = startedExpanding;

if (startedExpanding && [self.delegate respondsToSelector:@selector(shyNavBarManagerDidStartExpanding:)])
{
[self.delegate shyNavBarManagerDidStartExpanding:self];
}
}

- (void)setFullyContracted:(BOOL)fullyContracted
{
if (_fullyContracted == fullyContracted)
{
return;
}

_fullyContracted = fullyContracted;

if (fullyContracted && [self.delegate respondsToSelector:@selector(shyNavBarManagerDidBecomeFullyContracted:)])
{
[self.delegate shyNavBarManagerDidBecomeFullyContracted:self];
}
}

- (void)setFullyExpanded:(BOOL)fullyExpanded
{
if (_fullyExpanded == fullyExpanded)
{
return;
}

_fullyExpanded = fullyExpanded;

if (fullyExpanded && [self.delegate respondsToSelector:@selector(shyNavBarManagerDidBecomeFullyExpanded:)])
{
[self.delegate shyNavBarManagerDidBecomeFullyExpanded:self];
}
}


#pragma mark - Private methods

Expand Down Expand Up @@ -312,6 +402,7 @@ - (void)_handleScrolling

// 6 - Update the navigation bar shyViewController
self.navBarController.fadeBehavior = self.fadeBehavior;
self.navBarController.scale = self.scale;

// 7 - Inform the delegate if needed
CGFloat maxNavY = CGRectGetMaxY(self.navBarController.view.frame);
Expand All @@ -322,11 +413,11 @@ - (void)_handleScrolling
} else {
visibleTop = MAX(maxNavY, maxExtensionY);
}
if (visibleTop == self.statusBarController.calculateTotalHeightRecursively) {
if ([self.delegate respondsToSelector:@selector(shyNavBarManagerDidBecomeFullyContracted:)]) {
[self.delegate shyNavBarManagerDidBecomeFullyContracted:self];
}
}

self.startedContracting = deltaY < 0;
self.startedExpanding = deltaY > 0;
self.fullyContracted = visibleTop == self.statusBarController.calculateTotalHeightRecursively;
self.fullyExpanded = visibleTop == self.extensionController.calculateTotalHeightRecursively;

[self.navBarController updateYOffset:deltaY];
}
Expand Down Expand Up @@ -371,7 +462,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath
{
if (context == kTLYShyNavBarManagerKVOContext)
{
if (self.isViewControllerVisible && ![self _scrollViewIsSuffecientlyLong])
if (!self.isPageViewController && self.isViewControllerVisible && ![self _scrollViewIsSuffecientlyLong])
{
[self.navBarController expand];
}
Expand Down Expand Up @@ -409,11 +500,6 @@ - (void)setExtensionView:(UIView *)view
}
}

- (void)prepareForDisplay
{
[self cleanup];
}

- (void)layoutViews
{
if (fabs([self.scrollViewController updateLayoutIfNeeded]) > FLT_EPSILON)
Expand Down Expand Up @@ -493,13 +579,17 @@ + (void)load

- (void)tly_swizzledViewWillAppear:(BOOL)animated
{
[[self _internalShyNavBarManager] prepareForDisplay];
[[self _internalShyNavBarManager] cleanup];
[self tly_swizzledViewWillAppear:animated];
}

- (void)tly_swizzledViewDidLayoutSubviews
{
[[self _internalShyNavBarManager] layoutViews];
if (![self _internalShyNavBarManager].isPageViewController)
{
[[self _internalShyNavBarManager] layoutViews];
}

[self tly_swizzledViewDidLayoutSubviews];
}

Expand Down