如何将进度条添加到UIAlertController?

时间:2015-07-28 18:34:49

标签: ios swift progress-bar uialertcontroller

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

感谢

4 个答案:

答案 0 :(得分:17)

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

//  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的大小非常困难,但对于进度条,这应该可以解决问题。

答案 1 :(得分:3)

道歉,伙计们,在这个解决方案中使用目标c,但我认为它可能会帮助其他人,他们还没有使用Swift。而且,你也可以很容易地将它转换成Swift。这是我想强调的方法。

我也不确定Apple是否会拒绝这个解决方案,但无论如何都要这样做。

Apple表示,从iOS7开始,UIAlertView不应该是子类。 此类的视图层次结构是私有的,不得修改:

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

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

但是,我有一个解决方案,涉及在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首先完全显示。然后在正确的时刻添加一些其他委托函数来关闭UIProgressView:

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

- (void)alertViewCancel:(UIAlertView *)alertView{
    [self.progbar removeFromSuperview];
}

enter image description here

答案 2 :(得分:2)

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)
}

答案 3 :(得分:0)

具有自动布局的解决方案:

    UIAlertController *alertCtr = [UIAlertController alertControllerWithTitle:@"Test" message:@"50%" preferredStyle:UIAlertControllerStyleAlert];
    [alertCtr addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        // Do things
    }]];

    UIView *alertView = alertCtr.view;

    UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectZero];
    progressView.progress = 0.5;
    progressView.translatesAutoresizingMaskIntoConstraints = false;
    [alertView addSubview:progressView];


    NSLayoutConstraint *bottomConstraint = [progressView.bottomAnchor constraintEqualToAnchor:alertView.bottomAnchor];
    [bottomConstraint setActive:YES];
    bottomConstraint.constant = -45; // How to constraint to Cancel button?

    [[progressView.leftAnchor constraintEqualToAnchor:alertView.leftAnchor] setActive:YES];
    [[progressView.rightAnchor constraintEqualToAnchor:alertView.rightAnchor] setActive:YES];

    [self presentViewController:alertCtr animated:true completion:nil];

enter image description here