要在UIAlertView上显示UIProgressView
嗨,大家好,我想在UIAlertView显示UIProgressView显示文件的上传处理,但我已经search了太多,也find了该链接,但基层无法做到这一点。 我不明白这一点
如果有人知道最简单的方法,那么请让我知道。 我会很感激。
试试这个代码…
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil]; UIProgressView *pv = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar]; pv.frame = CGRectMake(20, 20, 200, 15); pv.progress = 0.5; [av addSubview:pv]; [av show];
我遇到了这个问题,结果是:
av = [[UIAlertView alloc] initWithTitle:@"Running" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar]; progressView.frame = CGRectMake(0, 0, 200, 15); progressView.bounds = CGRectMake(0, 0, 200, 15); progressView.backgroundColor = [UIColor blackColor]; [progressView setUserInteractionEnabled:NO]; [progressView setTrackTintColor:[UIColor blueColor]]; [progressView setProgressTintColor:[UIColor redColor]]; [av setValue:progressView forKey:@"accessoryView"]; [av show];
虽然这不能完全回答你的问题,但试试MBProgressHud ,这是一个内置此function的第三方控件。 在Github上提供的例子应该让你快速加速。
试试这个代码。 将YES设置为活动指示器,NO设置progressView
- (void) createProgressionAlertWithMessage:(NSString *)message withActivity:(BOOL)activity { progressAlert = [[UIAlertView alloc] initWithTitle: message message: @"Please wait..." delegate: self cancelButtonTitle: nil otherButtonTitles: nil]; // Create the progress bar and add it to the alert if (activity) { activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; activityView.frame = CGRectMake(139.0f-18.0f, 80.0f, 37.0f, 37.0f); [progressAlert addSubview:activityView]; [activityView startAnimating]; } else { progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(30.0f, 80.0f, 225.0f, 90.0f)]; [progressAlert addSubview:progressView]; [progressView setProgressViewStyle: UIProgressViewStyleBar]; } [progressAlert show]; [progressAlert release]; }
为什么不使用alerviewdelegate方法
- (void)willPresentAlertView:(UIAlertView *)alertView
这样做的好处是我们可以看到alertview实际上在屏幕上的大小,因为iOS已经预先计算了这个值,所以不需要魔术数字 – 或者覆盖苹果警告的类。
而从iOS7开始,我还记得从苹果公司的一些文档中看到,不要硬编码任何帧大小,而是总是从应用程序中计算出来,或者沿着这些线来计算它们。
- (void)willPresentAlertView:(UIAlertView *)alertView { CGRect alertRect = alertview.bounds; UIProgressView *loadingBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar]; loadingBar.bounds = CGRectMake(0, 0, alertRect.width, HEIGHT_YOU_WANT); // Do what ever you want here to set up the alertview, as you have all the details you need // Note the status Bar will always be there, haven't found a way of hiding it yet // Suggest adding an objective C reference to the original loading bar if you want to manipulate it further on don't forget to add #import <objc/runtime.h> objc_setAssociatedObject(alertView, &myKey, loadingBar, OBJC_ASSOCIATION_RETAIN); // Send the progressbar over to the alertview }
把参考拉到加载栏中
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
然后使用
UIProgressView *loadingBar = objc_getAssociatedObject(alertView, &myKey);
记得定义
#import <objc/runtime.h> static char myKey;
在类声明的顶部
这是创build一个警报视图
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@“Message”message:@“This is test”preferredStyle:UIAlertControllerStyleAlert];
现在添加文本框
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder=@"Enter Text label"; [textField setBorderStyle:UITextBorderStyleRoundedRect]; textField.backgroundColor=[UIColor whiteColor]; }];
并添加它的观点
[self presentViewController:alert animated:YES completion:nil];