我想在 UIAlertView 上显示 UIProgressView 以显示文件上传的处理过程。但是我搜索了太多,并且在该链接上也找到了,但仍然无法做到这一点。我不明白这个
如果有人知道最简单的方法,请告诉我。
我想在 UIAlertView 上显示 UIProgressView 以显示文件上传的处理过程。但是我搜索了太多,并且在该链接上也找到了,但仍然无法做到这一点。我不明白这个
如果有人知道最简单的方法,请告诉我。
我在这样做时遇到了问题,最后得到了这个:
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];
试试这个代码...
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];
虽然这并不能完全回答您的问题,但请尝试MBProgressHud,这是一个内置此功能的第三方控件。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
这样做的好处是我们可以看到警报视图在屏幕上的实际大小,因为 iOS 已经预先计算了这一点,所以不需要幻数 - 或者覆盖 Apple 警告的类!
从 iOS7 开始,我记得从 Apple 那里读过一些文件,说不要硬编码任何帧大小,而是总是从应用程序中计算它们,或者类似的东西?
- (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;
在你的类声明的顶部
这是创建一个警报视图
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];