Skip to content

Commit

Permalink
Merge pull request #113 from ilyavelilyaev/master
Browse files Browse the repository at this point in the history
Added ability to set custom background color to UINavigationBar progress bar.
  • Loading branch information
Marxon13 authored Nov 10, 2016
2 parents e6b7550 + a2f4e84 commit 5927f8f
Show file tree
Hide file tree
Showing 5 changed files with 371 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@
@param secondaryColor The color to set.
*/
- (void)setSecondaryColor:(UIColor *)secondaryColor;
/**
The background color of the progress bar, if nil, the background color will be the clearColor.
@param background The color to set.
*/
- (void)setBackgroundColor:(UIColor *)backgroundColor;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
static char isShowingProgressKey;
static char primaryColorKey;
static char secondaryColorKey;
static char backgroundColorKey;
static char backgroundViewKey;

@implementation UINavigationController (M13ProgressViewBar)

Expand Down Expand Up @@ -91,8 +93,8 @@ - (void)animateProgress:(CADisplayLink *)displayLink
- (void)finishProgress
{
UIView *progressView = [self getProgressView];

if (progressView) {
UIView *backgroundView = [self getBackgroundView];
if (progressView && backgroundView) {
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.1 animations:^{
CGRect progressFrame = progressView.frame;
Expand All @@ -101,8 +103,11 @@ - (void)finishProgress
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 animations:^{
progressView.alpha = 0;
backgroundView.alpha = 0;
} completion:^(BOOL finished) {
[progressView removeFromSuperview];
[backgroundView removeFromSuperview];
backgroundView.alpha = 1;
progressView.alpha = 1;
[self setTitle:nil];
[self setIsShowingProgressBar:NO];
Expand All @@ -115,14 +120,18 @@ - (void)finishProgress
- (void)cancelProgress
{
UIView *progressView = [self getProgressView];

if (progressView) {
UIView *backgroundView = [self getBackgroundView];

if (progressView && backgroundView) {
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.5 animations:^{
progressView.alpha = 0;
backgroundView.alpha = 0;
} completion:^(BOOL finished) {
[progressView removeFromSuperview];
[backgroundView removeFromSuperview];
progressView.alpha = 1;
backgroundView.alpha = 1;
[self setTitle:nil];
[self setIsShowingProgressBar:NO];
}];
Expand Down Expand Up @@ -154,9 +163,11 @@ - (UIInterfaceOrientation)currentDeviceOrientation
- (void)showProgress
{
UIView *progressView = [self getProgressView];
UIView *backgroundView = [self getBackgroundView];

[UIView animateWithDuration:.1 animations:^{
progressView.alpha = 1;
backgroundView.alpha = 1;
}];

[self setIsShowingProgressBar:YES];
Expand All @@ -174,14 +185,31 @@ - (void)updateProgressWithInterfaceOrientation:(UIInterfaceOrientation)interface
if(!progressView)
{
progressView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 2.5)];
progressView.backgroundColor = self.navigationBar.tintColor;
if ([self getPrimaryColor]) {
progressView.backgroundColor = [self getPrimaryColor];
}
progressView.clipsToBounds = YES;
[self setProgressView:progressView];
}

if ([self getPrimaryColor]) {
progressView.backgroundColor = [self getPrimaryColor];
} else {
progressView.backgroundColor = self.navigationBar.tintColor;
}

//Create background view if it doesn't exist
UIView *backgroundView = [self getBackgroundView];
if (!backgroundView)
{
backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 2.5)];
backgroundView.clipsToBounds = YES;
[self setBackgroundView:backgroundView];
}

if ([self getBackgroundColor]) {
backgroundView.backgroundColor = [self getBackgroundColor];
} else {
backgroundView.backgroundColor = [UIColor clearColor];
}

//Calculate the frame of the navigation bar, based off the orientation.
UIView *topView = self.topViewController.view;
CGSize screenSize;
Expand Down Expand Up @@ -209,6 +237,7 @@ - (void)updateProgressWithInterfaceOrientation:(UIInterfaceOrientation)interface

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

Expand All @@ -222,7 +251,7 @@ - (void)updateProgressWithInterfaceOrientation:(UIInterfaceOrientation)interface
//Calculate the width of the progress view
progressView.frame = CGRectMake(0, height - 2.5, width, 2.5);
}

backgroundView.frame = CGRectMake(0, height - 2.5, width, 2.5);
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
Expand Down Expand Up @@ -437,6 +466,17 @@ - (UIView *)getProgressView
return objc_getAssociatedObject(self, &progressViewKey);
}

- (void)setBackgroundView:(UIView *)view
{
objc_setAssociatedObject(self, &backgroundViewKey, view, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)getBackgroundView
{
return objc_getAssociatedObject(self, &backgroundViewKey);
}


- (void)setIndeterminate:(BOOL)indeterminate
{
objc_setAssociatedObject(self, &indeterminateKey, [NSNumber numberWithBool:indeterminate], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
Expand Down Expand Up @@ -488,4 +528,15 @@ - (UIColor *)getSecondaryColor
return objc_getAssociatedObject(self, &secondaryColorKey);
}

- (void)setBackgroundColor:(UIColor *)backgroundColor
{
objc_setAssociatedObject(self, &backgroundColorKey, backgroundColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self setIndeterminate:[self getIndeterminate]];
}

- (UIColor *)getBackgroundColor
{
return objc_getAssociatedObject(self, &backgroundColorKey);
}

@end
Loading

0 comments on commit 5927f8f

Please sign in to comment.