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

Use KVO to automatically respond correctly to changes in the extensionView size #124

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions TLYShyNavBar/TLYShyNavBarManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@

#import <objc/runtime.h>

/* _scrollViewIsSuffecientlyLong attempts to determine if the expansionView can be shown by virtue of
* excess space available in the scrollview relative to its content view. However, it breaks at edge cases,
* including if the content is just a bit more than threshold and then the user scrolls down further via
* the elastic scrollview... the logic suddenly thinks enough space is available and leads to the extention
* expanding.
*/
//#define USE_BROKEN_SCROLLVIEW_LENGTH_LOGIC

static void * const kTLYShyNavBarManagerKVOContext = (void*)&kTLYShyNavBarManagerKVOContext;

Expand Down Expand Up @@ -113,6 +120,7 @@ - (void)dealloc

[[NSNotificationCenter defaultCenter] removeObserver:self];
[_scrollView removeObserver:self forKeyPath:@"contentSize" context:kTLYShyNavBarManagerKVOContext];
[_extensionView removeObserver:self forKeyPath:@"frame" context:kTLYShyNavBarManagerKVOContext];
}

#pragma mark - Properties
Expand Down Expand Up @@ -221,9 +229,14 @@ - (void)setStickyExtensionView:(BOOL)stickyExtensionView

- (BOOL)_scrollViewIsSuffecientlyLong
{
#ifdef USE_BROKEN_SCROLLVIEW_LENGTH_LOGIC
CGRect scrollFrame = UIEdgeInsetsInsetRect(self.scrollView.bounds, self.scrollView.contentInset);
CGFloat scrollableAmount = self.scrollView.contentSize.height - CGRectGetHeight(scrollFrame);

return (scrollableAmount > [self.extensionController calculateTotalHeightRecursively]);
#else
return YES;
#endif
}

- (BOOL)_shouldHandleScrolling
Expand Down Expand Up @@ -357,9 +370,21 @@ - (void)observeValueForKeyPath:(NSString *)keyPath
{
if (context == kTLYShyNavBarManagerKVOContext)
{
if (self.isViewControllerVisible && ![self _scrollViewIsSuffecientlyLong])
{
[self.navBarController expand];
if (object == _scrollView) {
if (self.isViewControllerVisible && ![self _scrollViewIsSuffecientlyLong])
{
[self.navBarController expand];
}

} else if (object == _extensionView) {
self.extensionViewContainer.frame = _extensionView.frame;

/* Disable scroll handling temporarily while laying out views to avoid double-changing content
* offsets in _handleScrolling. */
BOOL wasDisabled = self.disable;
self.disable = YES;
[self layoutViews];
self.disable = wasDisabled;
}
}
else
Expand All @@ -374,6 +399,7 @@ - (void)setExtensionView:(UIView *)view
{
if (view != _extensionView)
{
[_extensionView removeObserver:self forKeyPath:@"frame" context:kTLYShyNavBarManagerKVOContext];
[_extensionView removeFromSuperview];
_extensionView = view;

Expand All @@ -386,12 +412,10 @@ - (void)setExtensionView:(UIView *)view
[self.extensionViewContainer addSubview:view];
self.extensionViewContainer.userInteractionEnabled = view.userInteractionEnabled;

/* Disable scroll handling temporarily while laying out views to avoid double-changing content
* offsets in _handleScrolling. */
BOOL wasDisabled = self.disable;
self.disable = YES;
[self layoutViews];
self.disable = wasDisabled;
// Update our extension container view and layout if _extensionView's bounds changes
[_extensionView addObserver:self forKeyPath:@"frame"
options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial)
context:kTLYShyNavBarManagerKVOContext];
}
}

Expand Down