I want to add progress bar in swift iOS 8 UIAlertController. Is this possible? Is there any way to subclass UIAlertController and add progres bar and connect some delegate funct
My apologies, guys, for using objective c, in this solution, but I thought it might help others, who do not use Swift yet. And, also, you can probably convert this quite easily into Swift. It is more the methodology I wanted to highlight.
I am also not sure whether Apple might reject this solution, but here goes anyway.
Apple states that from iOS7 onwards, UIAlertView should not be subclassed. The view hierarchy for this class is private and must not be modified:
https://developer.apple.com/reference/uikit/uialertview?language=objc
In other words, adding a UIView to a UIAlertView has absolutely no effect.
However, I have a solution that involves adding the UIProgressView above the UIAlertView, but adding the former to the app window. Using the UIView superview.center property, and some slight adjustments, the desired affect can be achieved:
-(void)addProgressBar{
float width = 232;
float height = 5;
self.progbar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
self.progbar.backgroundColor = [UIColor colorWithWhite:0.75f alpha:1.0f];
[self.progbar setFrame:CGRectMake(0,0,width,height)];
[self.progbar setTrackTintColor:[UIColor colorWithWhite:0.75f alpha:1.0f]];
[self.progbar setProgressTintColor:[UIColor colorWithRed:21.0f/255.0f green:126.0f/255.0f blue:251.0f/255.0f alpha:1.0f]];
self.progbar.alpha = 0.0;
[[UIApplication sharedApplication].keyWindow addSubview:self.progbar];
self.progbar.center = self.progbar.superview.center;
[self.progbar setFrame:CGRectMake(self.progbar.frame.origin.x,self.progbar.frame.origin.y+10,self.progbar.frame.size.width,self.progbar.frame.size.height)];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0];
[self.progbar setAlpha:1.0];
[UIView commitAnimations];
}
I add the fade in, to allow the UIAlertView to fully appear first. Then add some other delegate functions to dismiss the UIProgressView, at the correct moments:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(self.alert.cancelButtonIndex == buttonIndex){
[self.progbar removeFromSuperview];
}
}
- (void)alertViewCancel:(UIAlertView *)alertView{
[self.progbar removeFromSuperview];
}