如何将进度条添加到UIAlertController?

我想在swift iOS 8 UIAlertController中添加进度条。 这可能吗? 有没有办法inheritanceUIAlertController并添加progres栏和连接一些委托function?

谢谢

如果您只需要一个进度条,您可以简单地将其作为子视图添加,如下所示:

// Just create your alert as usual: let alertView = UIAlertController(title: "Please wait", message: "Need to download some files.", preferredStyle: .Alert) alertView.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)) // Show it to your users presentViewController(alertView, animated: true, completion: { // Add your progressbar after alert is shown (and measured) let margin:CGFloat = 8.0 let rect = CGRectMake(margin, 72.0, alertView.view.frame.width - margin * 2.0 , 2.0) let progressView = UIProgressView(frame: rect) progressView.progress = 0.5 progressView.tintColor = UIColor.blueColor() alertView.view.addSubview(progressView) }) 

为更大的内容调整UIAlertController大小是非常困难的,但是对于进度条来说,这应该是UIAlertController的。

对于在这个解决scheme中使用客观的c,我很抱歉,但是我认为它可以帮助其他人,谁还没有使用Swift。 而且,你也可以很容易地把它转换成Swift。 这是我想强调的方法。

我也不确定苹果是否会拒绝这个解决scheme,但无论如何,

苹果指出,从iOS7起,UIAlertView不应被分类。 这个类的视图层次是私有的,不能修改:

https://developer.apple.com/reference/uikit/uialertview?language=objc

换句话说,添加一个UIView到UIAlertView绝对没有效果。

但是,我有一个解决scheme,涉及在UIAlertView上添加UIProgressView,但将前者添加到应用程序窗口。 使用UIView superview.center属性和一些细微的调整,可以达到预期的效果:

 -(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]; } 

我添加淡入,让UIAlertView完全出现在第一位。 然后添加一些其他委托函数来closuresUIProgressView,在正确的时刻:

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if(self.alert.cancelButtonIndex == buttonIndex){ [self.progbar removeFromSuperview]; } } - (void)alertViewCancel:(UIAlertView *)alertView{ [self.progbar removeFromSuperview]; } 

在这里输入图像说明

 func downloadAlert() { let alertController = UIAlertController(title: "Title", message: "Loading...", preferredStyle: .Alert) let progressDownload : UIProgressView = UIProgressView(progressViewStyle: .Default) progressDownload.setProgress(5.0/10.0, animated: true) progressDownload.frame = CGRect(x: 10, y: 70, width: 250, height: 0) alertController.view.addSubview(progressDownload) presentViewController(alertController, animated: true, completion: nil) }