Skip to content

Commit 93772f7

Browse files
author
Ilya Velilyaev
committed
added background color to navigation bar progress view
1 parent c89079d commit 93772f7

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

Classes/NavigationController/UINavigationController+M13ProgressViewBar.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,11 @@
5050
@param secondaryColor The color to set.
5151
*/
5252
- (void)setSecondaryColor:(UIColor *)secondaryColor;
53+
/**
54+
The background color of the progress bar, if nil, the background color will be the clearColor.
55+
56+
@param background The color to set.
57+
*/
58+
- (void)setBackgroundColor:(UIColor *)backgroundColor;
5359

5460
@end

Classes/NavigationController/UINavigationController+M13ProgressViewBar.m

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
static char isShowingProgressKey;
2121
static char primaryColorKey;
2222
static char secondaryColorKey;
23+
static char backgroundColorKey;
24+
static char backgroundViewKey;
2325

2426
@implementation UINavigationController (M13ProgressViewBar)
2527

@@ -91,8 +93,8 @@ - (void)animateProgress:(CADisplayLink *)displayLink
9193
- (void)finishProgress
9294
{
9395
UIView *progressView = [self getProgressView];
94-
95-
if (progressView) {
96+
UIView *backgroundView = [self getBackgroundView];
97+
if (progressView && backgroundView) {
9698
dispatch_async(dispatch_get_main_queue(), ^{
9799
[UIView animateWithDuration:0.1 animations:^{
98100
CGRect progressFrame = progressView.frame;
@@ -101,8 +103,11 @@ - (void)finishProgress
101103
} completion:^(BOOL finished) {
102104
[UIView animateWithDuration:0.5 animations:^{
103105
progressView.alpha = 0;
106+
backgroundView.alpha = 0;
104107
} completion:^(BOOL finished) {
105108
[progressView removeFromSuperview];
109+
[backgroundView removeFromSuperview];
110+
backgroundView.alpha = 1;
106111
progressView.alpha = 1;
107112
[self setTitle:nil];
108113
[self setIsShowingProgressBar:NO];
@@ -115,14 +120,18 @@ - (void)finishProgress
115120
- (void)cancelProgress
116121
{
117122
UIView *progressView = [self getProgressView];
118-
119-
if (progressView) {
123+
UIView *backgroundView = [self getBackgroundView];
124+
125+
if (progressView && backgroundView) {
120126
dispatch_async(dispatch_get_main_queue(), ^{
121127
[UIView animateWithDuration:0.5 animations:^{
122128
progressView.alpha = 0;
129+
backgroundView.alpha = 0;
123130
} completion:^(BOOL finished) {
124131
[progressView removeFromSuperview];
132+
[backgroundView removeFromSuperview];
125133
progressView.alpha = 1;
134+
backgroundView.alpha = 1;
126135
[self setTitle:nil];
127136
[self setIsShowingProgressBar:NO];
128137
}];
@@ -154,9 +163,11 @@ - (UIInterfaceOrientation)currentDeviceOrientation
154163
- (void)showProgress
155164
{
156165
UIView *progressView = [self getProgressView];
166+
UIView *backgroundView = [self getBackgroundView];
157167

158168
[UIView animateWithDuration:.1 animations:^{
159169
progressView.alpha = 1;
170+
backgroundView.alpha = 1;
160171
}];
161172

162173
[self setIsShowingProgressBar:YES];
@@ -182,6 +193,19 @@ - (void)updateProgressWithInterfaceOrientation:(UIInterfaceOrientation)interface
182193
[self setProgressView:progressView];
183194
}
184195

196+
//Create background view if it doesn't exist
197+
UIView *backgroundView = [self getBackgroundView];
198+
if (!backgroundView)
199+
{
200+
backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 2.5)];
201+
backgroundView.backgroundColor = [UIColor clearColor];
202+
if ([self getBackgroundColor]) {
203+
backgroundView.backgroundColor = [self getBackgroundColor];
204+
}
205+
backgroundView.clipsToBounds = YES;
206+
[self setBackgroundView:backgroundView];
207+
}
208+
185209
//Calculate the frame of the navigation bar, based off the orientation.
186210
UIView *topView = self.topViewController.view;
187211
CGSize screenSize;
@@ -209,6 +233,7 @@ - (void)updateProgressWithInterfaceOrientation:(UIInterfaceOrientation)interface
209233

210234
//Check if the progress view is in its superview and if we are showing the bar.
211235
if (progressView.superview == nil && [self isShowingProgressBar]) {
236+
[self.navigationBar addSubview:backgroundView];
212237
[self.navigationBar addSubview:progressView];
213238
}
214239

@@ -222,7 +247,7 @@ - (void)updateProgressWithInterfaceOrientation:(UIInterfaceOrientation)interface
222247
//Calculate the width of the progress view
223248
progressView.frame = CGRectMake(0, height - 2.5, width, 2.5);
224249
}
225-
250+
backgroundView.frame = CGRectMake(0, height - 2.5, width, 2.5);
226251
}
227252

228253
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
@@ -437,6 +462,17 @@ - (UIView *)getProgressView
437462
return objc_getAssociatedObject(self, &progressViewKey);
438463
}
439464

465+
- (void)setBackgroundView:(UIView *)view
466+
{
467+
objc_setAssociatedObject(self, &backgroundViewKey, view, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
468+
}
469+
470+
- (UIView *)getBackgroundView
471+
{
472+
return objc_getAssociatedObject(self, &backgroundViewKey);
473+
}
474+
475+
440476
- (void)setIndeterminate:(BOOL)indeterminate
441477
{
442478
objc_setAssociatedObject(self, &indeterminateKey, [NSNumber numberWithBool:indeterminate], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
@@ -488,4 +524,15 @@ - (UIColor *)getSecondaryColor
488524
return objc_getAssociatedObject(self, &secondaryColorKey);
489525
}
490526

527+
- (void)setBackgroundColor:(UIColor *)backgroundColor
528+
{
529+
objc_setAssociatedObject(self, &backgroundColorKey, backgroundColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
530+
[self setIndeterminate:[self getIndeterminate]];
531+
}
532+
533+
- (UIColor *)getBackgroundColor
534+
{
535+
return objc_getAssociatedObject(self, &backgroundColorKey);
536+
}
537+
491538
@end

M13ProgressSuite/M13ProgressViewBarNavigationControllerViewController.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ - (void)viewDidLoad
3333
{
3434
[self.navigationController showProgress];
3535
[super viewDidLoad];
36+
[self.navigationController setBackgroundColor:[UIColor blackColor]];
3637
// Do any additional setup after loading the view.
3738
}
3839

0 commit comments

Comments
 (0)