Skip to content

Commit

Permalink
Fixed issue 65
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamazz committed Aug 22, 2014
1 parent 73d65b4 commit 0783bde
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 63 deletions.
7 changes: 7 additions & 0 deletions AMScrollingNavbar/UIViewController+ScrollingNavbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,12 @@
*/
- (void)setScrollingEnabled:(BOOL)enabled;

/** Enable or disable the scrolling when the content size is smaller than the bounds
*
* Set this property to YES to enable the scrolling of the navbar even when the
* content size of the scroll view is smaller than its height.
*/
- (void)setShouldScrollWhenContentFits:(BOOL)enabled;

@end

103 changes: 61 additions & 42 deletions AMScrollingNavbar/UIViewController+ScrollingNavbar.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ - (float)maxDelay { return [objc_getAssociatedObject(self, @selector(maxDelay))
- (void)setDelayDistance:(float)delayDistance { objc_setAssociatedObject(self, @selector(delayDistance), [NSNumber numberWithFloat:delayDistance], OBJC_ASSOCIATION_RETAIN); }
- (float)delayDistance { return [objc_getAssociatedObject(self, @selector(delayDistance)) floatValue]; }

- (void)setShouldScrollWhenContentFits:(BOOL)shouldScrollWhenContentFits { objc_setAssociatedObject(self, @selector(shouldScrollWhenContentFits), [NSNumber numberWithBool:shouldScrollWhenContentFits], OBJC_ASSOCIATION_RETAIN); }
- (BOOL)shouldScrollWhenContentFits { return [objc_getAssociatedObject(self, @selector(shouldScrollWhenContentFits)) boolValue]; }


- (void)followScrollView:(UIView*)scrollableView
{
Expand Down Expand Up @@ -88,6 +91,7 @@ - (void)followScrollView:(UIView*)scrollableView withDelay:(float)delay

self.maxDelay = delay;
self.delayDistance = delay;
self.shouldScrollWhenContentFits = NO;
}

- (void)stopFollowingScrollView
Expand Down Expand Up @@ -184,18 +188,43 @@ - (void)setScrollingEnabled:(BOOL)enabled

- (void)handlePan:(UIPanGestureRecognizer*)gesture
{
if (!self.shouldScrollWhenContentFits) {
if (self.scrollableView.frame.size.height >= [self contentSize].height) {
return;
}
}

CGPoint translation = [gesture translationInView:[self.scrollableView superview]];

float delta = self.lastContentOffset - translation.y;
self.lastContentOffset = translation.y;

[self scrollWithDelta:delta];

if ([gesture state] == UIGestureRecognizerStateEnded) {
// Reset the nav bar if the scroll is partial
self.lastContentOffset = 0;
[self checkForPartialScroll];
}
self.lastContentOffset = translation.y;

if ([self checkRubberbanding:delta]) {
[self scrollWithDelta:delta];
}

if ([gesture state] == UIGestureRecognizerStateEnded) {
// Reset the nav bar if the scroll is partial
[self checkForPartialScroll];
self.lastContentOffset = 0;
}
}

- (BOOL)checkRubberbanding:(CGFloat)delta
{
// Prevents the navbar from moving during the 'rubberband' scroll
if (delta < 0) {
if ([self contentoffset].y + self.scrollableView.frame.size.height > [self contentSize].height) {
if (self.scrollableView.frame.size.height < [self contentSize].height) { // Only if the content is big enough
return NO;
}
}
} else {
if ([self contentoffset].y < 0) {
return NO;
}
}
return YES;
}

- (void)scrollWithDelta:(CGFloat)delta
Expand All @@ -207,10 +236,6 @@ - (void)scrollWithDelta:(CGFloat)delta
return;
}

// Prevents the navbar from moving during the 'rubberband' scroll
if ([self contentoffset].y < 0) {
return;
}
if (self.expanded) {
self.expanded = NO;
}
Expand All @@ -236,12 +261,7 @@ - (void)scrollWithDelta:(CGFloat)delta
if (self.expanded) {
return;
}
// Prevents the navbar from moving during the 'rubberband' scroll
if ([self contentoffset].y + self.scrollableView.frame.size.height > [self contentSize].height) {
if (self.scrollableView.frame.size.height < [self contentSize].height) { // Only if the content is big enough
return;
}
}

if (self.collapsed) {
self.collapsed = NO;
}
Expand Down Expand Up @@ -307,48 +327,47 @@ - (CGSize)contentSize
- (void)checkForPartialScroll
{
CGFloat pos = self.navigationController.navigationBar.frame.origin.y;
__block CGRect frame = self.navigationController.navigationBar.frame;

// Get back down
if (pos >= -2) {
[UIView animateWithDuration:0.2 animations:^{
CGRect frame;
frame = self.navigationController.navigationBar.frame;
CGFloat delta = frame.origin.y - self.statusBar;
frame.origin.y = MIN(20, frame.origin.y - delta);
if (pos >= (self.statusBar - frame.size.height / 2)) {
CGFloat delta = frame.origin.y - self.statusBar;
NSTimeInterval duration = ABS((delta / (frame.size.height / 2)) * 0.2);
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
frame.origin.y = self.statusBar;
self.navigationController.navigationBar.frame = frame;

self.expanded = YES;
self.collapsed = NO;

[self updateSizingWithDelta:delta];
}];
} completion:nil];
} else {
// And back up
[UIView animateWithDuration:0.2 animations:^{
CGRect frame;
frame = self.navigationController.navigationBar.frame;
CGFloat delta = frame.origin.y + self.deltaLimit;
frame.origin.y = MAX(-self.deltaLimit, frame.origin.y - delta);
CGFloat delta = frame.origin.y + self.deltaLimit;
NSTimeInterval duration = ABS((delta / (frame.size.height / 2)) * 0.2);
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
frame.origin.y = -self.deltaLimit;
self.navigationController.navigationBar.frame = frame;

self.expanded = NO;
self.collapsed = YES;
self.delayDistance = self.maxDelay;

[self updateSizingWithDelta:delta];
}];
} completion:nil];
}
}

- (void)updateSizingWithDelta:(CGFloat)delta
{
[self updateNavbarAlpha:delta];

// At this point the navigation bar is already been placed in the right position, it'll be the reference point for the other views'sizing
CGRect frameNav = self.navigationController.navigationBar.frame;
// Move and expand (or shrink) the superview of the given scrollview
CGRect frame = self.scrollableView.superview.frame;
[self updateNavbarAlpha:delta];
// At this point the navigation bar is already been placed in the right position, it'll be the reference point for the other views'sizing
CGRect frameNav = self.navigationController.navigationBar.frame;
// Move and expand (or shrink) the superview of the given scrollview
CGRect frame = self.scrollableView.superview.frame;
if (IOS7_OR_LATER) {
frame.origin.y = frameNav.origin.y + frameNav.size.height;
} else {
Expand All @@ -359,7 +378,7 @@ - (void)updateSizingWithDelta:(CGFloat)delta
} else {
frame.size.height = [UIScreen mainScreen].bounds.size.height - [self statusBar];
}
self.scrollableView.superview.frame = frame;
self.scrollableView.superview.frame = frame;
self.scrollableView.frame = self.scrollableView.superview.bounds;
[self.view setNeedsLayout];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
isa = PBXProject;
attributes = {
CLASSPREFIX = AM;
LastUpgradeCheck = 0510;
LastUpgradeCheck = 0600;
ORGANIZATIONNAME = "Andrea Mazzini";
TargetAttributes = {
652B448C182D376600D01F5C = {
Expand Down Expand Up @@ -382,7 +382,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
ONLY_ACTIVE_ARCH = NO;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
VALID_ARCHS = "armv7 armv7s";
};
Expand Down Expand Up @@ -424,7 +424,6 @@
652B449F182D376600D01F5C /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD)";
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
Expand All @@ -433,15 +432,14 @@
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
PRODUCT_NAME = "$(TARGET_NAME)";
TARGETED_DEVICE_FAMILY = 1;
VALID_ARCHS = "armv7 armv7s i386";
VALID_ARCHS = "armv7 armv7s i386 x86_64";
WRAPPER_EXTENSION = app;
};
name = Debug;
};
652B44A0182D376600D01F5C /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD)";
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
Expand All @@ -450,7 +448,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
PRODUCT_NAME = "$(TARGET_NAME)";
TARGETED_DEVICE_FAMILY = 1;
VALID_ARCHS = "armv7 armv7s i386";
VALID_ARCHS = "armv7 armv7s i386 x86_64";
WRAPPER_EXTENSION = app;
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0510"
LastUpgradeVersion = "0600"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ - (void)viewDidLoad

[self setTitle:@"Table View"];

self.data = @[@"Awesome content", @"Great content", @"Amazing content", @"Ludicrous content", @"Awesome content", @"Great content", @"Amazing content", @"Ludicrous content", @"Awesome content", @"Great content", @"Amazing content", @"Ludicrous content", @"Awesome content", @"Great content", @"Amazing content", @"Ludicrous content"];
self.data = @[@"Awesome content", @"Great content"]; //, @"Amazing content", @"Ludicrous content", @"Awesome content", @"Great content", @"Amazing content", @"Ludicrous content", @"Awesome content", @"Great content", @"Amazing content", @"Ludicrous content", @"Awesome content", @"Great content", @"Amazing content", @"Ludicrous content"];

[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
Expand All @@ -34,6 +34,9 @@ - (void)viewDidLoad

// Just call this line to enable the scrolling navbar
[self followScrollView:self.tableView];

[self setShouldScrollWhenContentFits:YES];

}

- (void)viewWillDisappear:(BOOL)animated
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x",
"filename" : "nav.png"
},
{
"idiom" : "universal",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 6 additions & 13 deletions ScrollingNavbarDemo/ScrollingNavbarDemo/en.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5056" systemVersion="13C1021" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="bcL-UJ-dqI">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6206.8" systemVersion="14A329f" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="bcL-UJ-dqI">
<dependencies>
<deployment defaultVersion="1536" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7026.1"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -19,7 +18,6 @@
<subviews>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="9Iy-z5-iHz">
<rect key="frame" x="0.0" y="9" width="320" height="44"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" red="0.22768095269999999" green="0.3203378053" blue="0.4262695312" alpha="1" colorSpace="calibratedRGB"/>
<fontDescription key="fontDescription" name="Futura-Medium" family="Futura" pointSize="15"/>
<state key="normal" title="Scroll View">
Expand All @@ -32,7 +30,6 @@
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="tUD-O8-N3a">
<rect key="frame" x="0.0" y="62" width="320" height="44"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" red="0.22768095269999999" green="0.3203378053" blue="0.4262695312" alpha="1" colorSpace="calibratedRGB"/>
<fontDescription key="fontDescription" name="Futura-Medium" family="Futura" pointSize="15"/>
<state key="normal" title="Table View">
Expand All @@ -45,7 +42,6 @@
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="rg7-vb-HUK">
<rect key="frame" x="0.0" y="115" width="320" height="44"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" red="0.22768095269999999" green="0.3203378053" blue="0.4262695312" alpha="1" colorSpace="calibratedRGB"/>
<fontDescription key="fontDescription" name="Futura-Medium" family="Futura" pointSize="15"/>
<state key="normal" title="Web View">
Expand All @@ -58,7 +54,6 @@
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="8zp-gR-gPv">
<rect key="frame" x="0.0" y="168" width="320" height="44"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" red="0.22768095269999999" green="0.3203378053" blue="0.4262695312" alpha="1" colorSpace="calibratedRGB"/>
<fontDescription key="fontDescription" name="Futura-Medium" family="Futura" pointSize="15"/>
<state key="normal" title="Collection View">
Expand Down Expand Up @@ -93,7 +88,6 @@
<subviews>
<webView contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="EAg-Eb-eK5">
<rect key="frame" x="0.0" y="0.0" width="320" height="504"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</webView>
</subviews>
Expand Down Expand Up @@ -139,7 +133,6 @@
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Example Cell" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="2" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="114" translatesAutoresizingMaskIntoConstraints="NO" id="rlr-1Q-OM4">
<rect key="frame" x="20" y="20" width="114" height="114"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" red="0.41960784309999999" green="0.58823529409999997" blue="0.78039215689999997" alpha="1" colorSpace="calibratedRGB"/>
<fontDescription key="fontDescription" name="Futura-Medium" family="Futura" pointSize="21"/>
<color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
Expand Down Expand Up @@ -177,15 +170,13 @@
<subviews>
<searchBar contentMode="redraw" translatesAutoresizingMaskIntoConstraints="NO" id="lln-rf-2it">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="HF4-Yc-LRs" id="ibW-LM-cvM"/>
</connections>
</searchBar>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" translatesAutoresizingMaskIntoConstraints="NO" id="5DA-Q0-AzK">
<rect key="frame" x="0.0" y="44" width="320" height="460"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="width" constant="320" id="Iqn-LP-PSU"/>
Expand Down Expand Up @@ -251,7 +242,6 @@
<subviews>
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="1kz-fZ-0sS">
<rect key="frame" x="0.0" y="0.0" width="320" height="504"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
</scrollView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
Expand Down Expand Up @@ -294,6 +284,9 @@
<simulatedMetricsContainer key="defaultSimulatedMetrics">
<simulatedStatusBarMetrics key="statusBar"/>
<simulatedOrientationMetrics key="orientation"/>
<simulatedScreenMetrics key="destination" type="retina4"/>
<simulatedScreenMetrics key="destination" type="retina4">
<size key="portraitSize" width="320" height="568"/>
<size key="landscapeSize" width="568" height="320"/>
</simulatedScreenMetrics>
</simulatedMetricsContainer>
</document>

0 comments on commit 0783bde

Please sign in to comment.