Skip to content

Commit 4ad45e0

Browse files
committed
Added generic view compatibility
1 parent 1428941 commit 4ad45e0

File tree

21 files changed

+384
-60
lines changed

21 files changed

+384
-60
lines changed

AMScrollingNavbar/AMScrollingNavbarViewController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
@interface AMScrollingNavbarViewController : UIViewController
1212

13-
- (void)followScrollView:(UIScrollView*)scrollView;
13+
- (void)followScrollView:(UIView*)scrollView;
1414

1515
@end

AMScrollingNavbar/AMScrollingNavbarViewController.m

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
@interface AMScrollingNavbarViewController () <UIGestureRecognizerDelegate>
1212

13-
@property (nonatomic, weak) UIScrollView* scrollView;
13+
@property (nonatomic, weak) UIView* scrollableView;
1414
@property (assign, nonatomic) float lastContentOffset;
1515
@property (strong, nonatomic) UIPanGestureRecognizer* panGesture;
1616
@property (strong, nonatomic) UIView* overlay;
@@ -21,18 +21,16 @@ @interface AMScrollingNavbarViewController () <UIGestureRecognizerDelegate>
2121

2222
@implementation AMScrollingNavbarViewController
2323

24-
- (void)followScrollView:(UIScrollView*)scrollView
24+
- (void)followScrollView:(UIView*)scrollView
2525
{
26-
self.scrollView = scrollView;
26+
self.scrollableView = scrollView;
2727

2828
self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
2929
[self.panGesture setMaximumNumberOfTouches:1];
3030

3131
[self.panGesture setDelegate:self];
32-
[self.scrollView addGestureRecognizer:self.panGesture];
33-
34-
[self.navigationController.navigationBar setTranslucent:NO];
35-
32+
[self.scrollableView addGestureRecognizer:self.panGesture];
33+
3634
CGRect frame = self.navigationController.navigationBar.frame;
3735
frame.origin = CGPointZero;
3836
self.overlay = [[UIView alloc] initWithFrame:frame];
@@ -52,7 +50,7 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecogni
5250

5351
- (void)handlePan:(UIPanGestureRecognizer*)gesture
5452
{
55-
CGPoint translation = [gesture translationInView:[self.scrollView superview]];
53+
CGPoint translation = [gesture translationInView:[self.scrollableView superview]];
5654

5755
float delta = self.lastContentOffset - translation.y;
5856
self.lastContentOffset = translation.y;
@@ -81,7 +79,9 @@ - (void)handlePan:(UIPanGestureRecognizer*)gesture
8179
[self updateSizingWithDelta:delta];
8280

8381
// Keeps the view's scroll position steady until the navbar is gone
84-
[self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentOffset.y - delta)];
82+
if ([self.scrollableView isKindOfClass:[UIScrollView class]]) {
83+
[(UIScrollView*)self.scrollableView setContentOffset:CGPointMake(((UIScrollView*)self.scrollableView).contentOffset.x, ((UIScrollView*)self.scrollableView).contentOffset.y - delta)];
84+
}
8585
}
8686

8787
if (delta < 0) {
@@ -156,15 +156,17 @@ - (void)updateSizingWithDelta:(CGFloat)delta
156156

157157
float alpha = (frame.origin.y + 24) / frame.size.height;
158158
[self.overlay setAlpha:1 - alpha];
159-
160-
frame = self.scrollView.superview.frame;
159+
self.navigationController.navigationBar.tintColor = [self.navigationController.navigationBar.tintColor colorWithAlphaComponent:alpha];
160+
161+
frame = self.scrollableView.superview.frame;
161162
frame.origin.y = self.navigationController.navigationBar.frame.origin.y + self.navigationController.navigationBar.frame.size.height;
162163
frame.size.height = frame.size.height + delta;
163-
self.scrollView.superview.frame = frame;
164+
self.scrollableView.superview.frame = frame;
164165

165-
frame = self.scrollView.frame;
166+
// Changing the layer's frame avoids UIWebView's glitchiness
167+
frame = self.scrollableView.layer.frame;
166168
frame.size.height += delta;
167-
self.scrollView.frame = frame;
169+
self.scrollableView.layer.frame = frame;
168170
}
169171

170172

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,26 @@ To enable the scrolling effect you simply need to call followScrollView: providi
3333
[self followScrollView:self.scrollView];
3434
```
3535
36-
Make sure to have a barTintColor for your UINavigationBar, or you won't see the fade-in and fade-out effects.
36+
Make sure to have a ```barTintColor``` for your ```UINavigationBar```, or you won't see the fade-in and fade-out effects.
37+
Also make sure that you are not using a translucent navigation bar. E.g., in your controller:
38+
```objc
39+
[self.navigationController.navigationBar setTranslucent:NO];
40+
```
3741

3842
Changelog
3943
==================
4044

45+
0.2
46+
--------------------
47+
- Added support for generic UIViews
48+
4149
0.1
4250
--------------------
4351
- Initial Version.
4452

4553
TODO
4654
--------------------
47-
* Check compatibility with UITableView and other scroll view based components.
55+
* Needs testing
4856

4957
MIT License
5058
--------------------

ScrollingNavbarDemo/ScrollingNavbarDemo.xcodeproj/project.pbxproj

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
652B4499182D376600D01F5C /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 652B4497182D376600D01F5C /* InfoPlist.strings */; };
2323
652B449B182D376600D01F5C /* ScrollingNavbarDemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 652B449A182D376600D01F5C /* ScrollingNavbarDemoTests.m */; };
2424
652B44AA182D48DE00D01F5C /* AMScrollingNavbarViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 652B44A9182D48DE00D01F5C /* AMScrollingNavbarViewController.m */; };
25+
65BF9E69182E5AB70092BF7F /* AMScrollViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 65BF9E68182E5AB70092BF7F /* AMScrollViewController.m */; };
26+
65BF9E6C182E5AC70092BF7F /* AMWebViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 65BF9E6B182E5AC70092BF7F /* AMWebViewController.m */; };
27+
65BF9E6F182E5AD40092BF7F /* AMTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 65BF9E6E182E5AD40092BF7F /* AMTableViewController.m */; };
2528
/* End PBXBuildFile section */
2629

2730
/* Begin PBXContainerItemProxy section */
@@ -56,6 +59,12 @@
5659
652B449A182D376600D01F5C /* ScrollingNavbarDemoTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ScrollingNavbarDemoTests.m; sourceTree = "<group>"; };
5760
652B44A8182D48DE00D01F5C /* AMScrollingNavbarViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AMScrollingNavbarViewController.h; sourceTree = "<group>"; };
5861
652B44A9182D48DE00D01F5C /* AMScrollingNavbarViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AMScrollingNavbarViewController.m; sourceTree = "<group>"; };
62+
65BF9E67182E5AB70092BF7F /* AMScrollViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AMScrollViewController.h; sourceTree = "<group>"; };
63+
65BF9E68182E5AB70092BF7F /* AMScrollViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AMScrollViewController.m; sourceTree = "<group>"; };
64+
65BF9E6A182E5AC70092BF7F /* AMWebViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AMWebViewController.h; sourceTree = "<group>"; };
65+
65BF9E6B182E5AC70092BF7F /* AMWebViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AMWebViewController.m; sourceTree = "<group>"; };
66+
65BF9E6D182E5AD40092BF7F /* AMTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AMTableViewController.h; sourceTree = "<group>"; };
67+
65BF9E6E182E5AD40092BF7F /* AMTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AMTableViewController.m; sourceTree = "<group>"; };
5968
/* End PBXFileReference section */
6069

6170
/* Begin PBXFrameworksBuildPhase section */
@@ -119,8 +128,7 @@
119128
652B447E182D376600D01F5C /* AMAppDelegate.h */,
120129
652B447F182D376600D01F5C /* AMAppDelegate.m */,
121130
652B4481182D376600D01F5C /* Main.storyboard */,
122-
652B4484182D376600D01F5C /* AMViewController.h */,
123-
652B4485182D376600D01F5C /* AMViewController.m */,
131+
65BF9E66182E5A9C0092BF7F /* ViewControllers */,
124132
652B4487182D376600D01F5C /* Images.xcassets */,
125133
652B4476182D376600D01F5C /* Supporting Files */,
126134
);
@@ -166,6 +174,21 @@
166174
path = ../../AMScrollingNavbar;
167175
sourceTree = "<group>";
168176
};
177+
65BF9E66182E5A9C0092BF7F /* ViewControllers */ = {
178+
isa = PBXGroup;
179+
children = (
180+
652B4484182D376600D01F5C /* AMViewController.h */,
181+
652B4485182D376600D01F5C /* AMViewController.m */,
182+
65BF9E67182E5AB70092BF7F /* AMScrollViewController.h */,
183+
65BF9E68182E5AB70092BF7F /* AMScrollViewController.m */,
184+
65BF9E6A182E5AC70092BF7F /* AMWebViewController.h */,
185+
65BF9E6B182E5AC70092BF7F /* AMWebViewController.m */,
186+
65BF9E6D182E5AD40092BF7F /* AMTableViewController.h */,
187+
65BF9E6E182E5AD40092BF7F /* AMTableViewController.m */,
188+
);
189+
name = ViewControllers;
190+
sourceTree = "<group>";
191+
};
169192
/* End PBXGroup section */
170193

171194
/* Begin PBXNativeTarget section */
@@ -264,8 +287,11 @@
264287
isa = PBXSourcesBuildPhase;
265288
buildActionMask = 2147483647;
266289
files = (
290+
65BF9E6F182E5AD40092BF7F /* AMTableViewController.m in Sources */,
267291
652B4480182D376600D01F5C /* AMAppDelegate.m in Sources */,
292+
65BF9E6C182E5AC70092BF7F /* AMWebViewController.m in Sources */,
268293
652B44AA182D48DE00D01F5C /* AMScrollingNavbarViewController.m in Sources */,
294+
65BF9E69182E5AB70092BF7F /* AMScrollViewController.m in Sources */,
269295
652B4486182D376600D01F5C /* AMViewController.m in Sources */,
270296
652B447C182D376600D01F5C /* main.m in Sources */,
271297
);

ScrollingNavbarDemo/ScrollingNavbarDemo/AMAppDelegate.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ @implementation AMAppDelegate
1212

1313
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
1414
{
15-
// Override point for customization after application launch.
1615
return YES;
1716
}
1817

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// AMScrollViewController.h
3+
// ScrollingNavbarDemo
4+
//
5+
// Created by Andrea Mazzini on 09/11/13.
6+
// Copyright (c) 2013 Andrea Mazzini. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
#import "AMScrollingNavbarViewController.h"
11+
12+
@interface AMScrollViewController : AMScrollingNavbarViewController
13+
14+
@end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// AMScrollViewController.m
3+
// ScrollingNavbarDemo
4+
//
5+
// Created by Andrea Mazzini on 09/11/13.
6+
// Copyright (c) 2013 Andrea Mazzini. All rights reserved.
7+
//
8+
9+
#import "AMScrollViewController.h"
10+
11+
@interface AMScrollViewController ()
12+
13+
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
14+
15+
@end
16+
17+
@implementation AMScrollViewController
18+
19+
- (void)viewDidLoad
20+
{
21+
[super viewDidLoad];
22+
23+
[self setTitle:@"Scroll View"];
24+
25+
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 320, 40)];
26+
[label setText:@"My content"];
27+
[label setTextAlignment:NSTextAlignmentCenter];
28+
[label setTextColor:[UIColor whiteColor]];
29+
[self.scrollView addSubview:label];
30+
[self.view setBackgroundColor:[UIColor colorWithRed:80.0/255.0 green:110.0/255.0 blue:130.0/255.0 alpha:1]];
31+
[self.scrollView setBackgroundColor:[UIColor colorWithRed:80.0/255.0 green:110.0/255.0 blue:130.0/255.0 alpha:1]];
32+
33+
// Let's fake some content
34+
[self.scrollView setContentSize:CGSizeMake(320, 840)];
35+
36+
// Set the barTintColor. This will determine the overlay that fades in and out upon scrolling.
37+
[self.navigationController.navigationBar setBarTintColor:[UIColor orangeColor]];
38+
39+
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
40+
41+
// Just call this line to enable the scrolling navbar
42+
[self followScrollView:self.scrollView];
43+
}
44+
45+
@end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// AMTableViewController.h
3+
// ScrollingNavbarDemo
4+
//
5+
// Created by Andrea Mazzini on 09/11/13.
6+
// Copyright (c) 2013 Andrea Mazzini. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
#import "AMScrollingNavbarViewController.h"
11+
12+
@interface AMTableViewController : AMScrollingNavbarViewController
13+
14+
@end
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// AMTableViewController.m
3+
// ScrollingNavbarDemo
4+
//
5+
// Created by Andrea Mazzini on 09/11/13.
6+
// Copyright (c) 2013 Andrea Mazzini. All rights reserved.
7+
//
8+
9+
#import "AMTableViewController.h"
10+
11+
@interface AMTableViewController () <UITableViewDataSource, UITableViewDelegate>
12+
13+
@property (weak, nonatomic) IBOutlet UITableView *tableView;
14+
15+
@end
16+
17+
@implementation AMTableViewController
18+
19+
- (void)viewDidLoad
20+
{
21+
[super viewDidLoad];
22+
23+
[self setTitle:@"Table View"];
24+
25+
[self.tableView setDelegate:self];
26+
[self.tableView setDataSource:self];
27+
28+
// Set the barTintColor. This will determine the overlay that fades in and out upon scrolling.
29+
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:60.0/255.0 green:1 blue:150.0/255.0 alpha:1]];
30+
31+
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
32+
33+
// Just call this line to enable the scrolling navbar
34+
[self followScrollView:self.tableView];
35+
}
36+
37+
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
38+
{
39+
return 20;
40+
}
41+
42+
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
43+
{
44+
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"];
45+
if (!cell) {
46+
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Identifier"];
47+
}
48+
49+
cell.textLabel.text = @[@"Awesome content", @"Great content", @"Amazing content", @"Ludicrous content"][arc4random()%4];
50+
51+
return cell;
52+
}
53+
54+
@end

ScrollingNavbarDemo/ScrollingNavbarDemo/AMViewController.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
//
88

99
#import <UIKit/UIKit.h>
10-
#import "AMScrollingNavbarViewController.h"
1110

12-
@interface AMViewController : AMScrollingNavbarViewController
11+
@interface AMViewController : UIViewController
1312

1413
@end

0 commit comments

Comments
 (0)