2015-07-28 17 views

Trả lời

14

Nếu bạn chỉ cần một progressbar bạn chỉ có thể thêm nó như là một subview như sau:

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

Đó là khá khó khăn để thay đổi kích thước UIAlertController cho nội dung lớn hơn nhưng đối với một progressbar này nên làm lừa.

+0

Làm thế nào để bạn cập nhật những tiến bộ? –

+2

Bạn chỉ cần thay đổi "progressView.progress". Có thể rõ ràng hơn để xác định "var progressView: UIProgressView?" bên ngoài (không phải trong khối hoàn thành) là tùy chọn và sau đó đặt "progressView? .progress = 0.123". – coyer

+0

tại sao yo sử dụng hằng số? điều này chỉ nhận được kết quả trực quan khác nhau trên iPhone và iPad. Trên thực tế, chỉ có vẻ tốt trong chân dung iPhone vì trong bối cảnh progressview trông chuyển – Ricardo

3
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) 
} 
+0

Tôi chạy ứng dụng và nhận được lỗi này *** Chấm dứt ứng dụng do ngoại lệ vô tình 'NSInternalInconsistencyException', lý do: 'Không thể xác định hướng điều hướng để cuộn' *** Lệnh gọi ném đầu tiên: –

4

Xin lỗi, các bạn, vì đã sử dụng mục tiêu c, trong giải pháp này, nhưng tôi nghĩ nó có thể giúp những người khác, những người chưa sử dụng Swift. Và, ngoài ra, bạn có thể chuyển đổi nó khá dễ dàng thành Swift. Đó là phương pháp tôi muốn làm nổi bật hơn.

Tôi cũng không chắc liệu Apple có thể từ chối giải pháp này hay không, nhưng ở đây cũng vậy.

Apple tuyên bố rằng từ iOS7 trở đi, UIAlertView không nên được phân loại. Quan điểm phân cấp cho lớp này là tư nhân và không được chỉnh sửa:

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

Nói cách khác, việc thêm một UIView để một UIAlertView hoàn toàn không có hiệu lực.

Tuy nhiên, tôi có giải pháp liên quan đến việc thêm UIProgressView phía trên UIAlertView, nhưng thêm phần trước vào cửa sổ ứng dụng. Sử dụng UIView superview.center bất động sản, và một số điều chỉnh nhỏ, mong muốn ảnh hưởng có thể đạt được:

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

tôi thêm các fade in, cho phép UIAlertView xuất hiện đầy đủ đầu tiên. Sau đó thêm một số chức năng đại biểu khác để bỏ qua những UIProgressView, tại những khoảnh khắc chính xác:

- (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

+0

có thể viết mã nhanh tại đây? –

+0

@SaeedRahmatolahi Xin lỗi, Saeed, tôi e rằng tôi là người trượt ván cũ. Chỉ biết mục tiêu c, vì vậy tôi không thể chuyển đổi điều này cho bạn ... –

+0

:(ok cảm ơn vì đã đọc câu hỏi của tôi –

Các vấn đề liên quan