20
20
static char isShowingProgressKey;
21
21
static char primaryColorKey;
22
22
static char secondaryColorKey;
23
+ static char backgroundColorKey;
24
+ static char backgroundViewKey;
23
25
24
26
@implementation UINavigationController (M13ProgressViewBar)
25
27
@@ -91,8 +93,8 @@ - (void)animateProgress:(CADisplayLink *)displayLink
91
93
- (void )finishProgress
92
94
{
93
95
UIView *progressView = [self getProgressView ];
94
-
95
- if (progressView) {
96
+ UIView *backgroundView = [ self getBackgroundView ];
97
+ if (progressView && backgroundView ) {
96
98
dispatch_async (dispatch_get_main_queue (), ^{
97
99
[UIView animateWithDuration: 0.1 animations: ^{
98
100
CGRect progressFrame = progressView.frame ;
@@ -101,8 +103,11 @@ - (void)finishProgress
101
103
} completion: ^(BOOL finished) {
102
104
[UIView animateWithDuration: 0.5 animations: ^{
103
105
progressView.alpha = 0 ;
106
+ backgroundView.alpha = 0 ;
104
107
} completion: ^(BOOL finished) {
105
108
[progressView removeFromSuperview ];
109
+ [backgroundView removeFromSuperview ];
110
+ backgroundView.alpha = 1 ;
106
111
progressView.alpha = 1 ;
107
112
[self setTitle: nil ];
108
113
[self setIsShowingProgressBar: NO ];
@@ -115,14 +120,18 @@ - (void)finishProgress
115
120
- (void )cancelProgress
116
121
{
117
122
UIView *progressView = [self getProgressView ];
118
-
119
- if (progressView) {
123
+ UIView *backgroundView = [self getBackgroundView ];
124
+
125
+ if (progressView && backgroundView) {
120
126
dispatch_async (dispatch_get_main_queue (), ^{
121
127
[UIView animateWithDuration: 0.5 animations: ^{
122
128
progressView.alpha = 0 ;
129
+ backgroundView.alpha = 0 ;
123
130
} completion: ^(BOOL finished) {
124
131
[progressView removeFromSuperview ];
132
+ [backgroundView removeFromSuperview ];
125
133
progressView.alpha = 1 ;
134
+ backgroundView.alpha = 1 ;
126
135
[self setTitle: nil ];
127
136
[self setIsShowingProgressBar: NO ];
128
137
}];
@@ -154,9 +163,11 @@ - (UIInterfaceOrientation)currentDeviceOrientation
154
163
- (void )showProgress
155
164
{
156
165
UIView *progressView = [self getProgressView ];
166
+ UIView *backgroundView = [self getBackgroundView ];
157
167
158
168
[UIView animateWithDuration: .1 animations: ^{
159
169
progressView.alpha = 1 ;
170
+ backgroundView.alpha = 1 ;
160
171
}];
161
172
162
173
[self setIsShowingProgressBar: YES ];
@@ -182,6 +193,19 @@ - (void)updateProgressWithInterfaceOrientation:(UIInterfaceOrientation)interface
182
193
[self setProgressView: progressView];
183
194
}
184
195
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
+
185
209
// Calculate the frame of the navigation bar, based off the orientation.
186
210
UIView *topView = self.topViewController .view ;
187
211
CGSize screenSize;
@@ -209,6 +233,7 @@ - (void)updateProgressWithInterfaceOrientation:(UIInterfaceOrientation)interface
209
233
210
234
// Check if the progress view is in its superview and if we are showing the bar.
211
235
if (progressView.superview == nil && [self isShowingProgressBar ]) {
236
+ [self .navigationBar addSubview: backgroundView];
212
237
[self .navigationBar addSubview: progressView];
213
238
}
214
239
@@ -222,7 +247,7 @@ - (void)updateProgressWithInterfaceOrientation:(UIInterfaceOrientation)interface
222
247
// Calculate the width of the progress view
223
248
progressView.frame = CGRectMake (0 , height - 2.5 , width, 2.5 );
224
249
}
225
-
250
+ backgroundView. frame = CGRectMake ( 0 , height - 2.5 , width, 2.5 );
226
251
}
227
252
228
253
- (void )willRotateToInterfaceOrientation : (UIInterfaceOrientation)toInterfaceOrientation duration : (NSTimeInterval )duration
@@ -437,6 +462,17 @@ - (UIView *)getProgressView
437
462
return objc_getAssociatedObject (self, &progressViewKey);
438
463
}
439
464
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
+
440
476
- (void )setIndeterminate : (BOOL )indeterminate
441
477
{
442
478
objc_setAssociatedObject (self, &indeterminateKey, [NSNumber numberWithBool: indeterminate], OBJC_ASSOCIATION_RETAIN_NONATOMIC );
@@ -488,4 +524,15 @@ - (UIColor *)getSecondaryColor
488
524
return objc_getAssociatedObject (self, &secondaryColorKey);
489
525
}
490
526
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
+
491
538
@end
0 commit comments