want to display UIProgressView on UIAlertView

随声附和 提交于 2019-11-30 15:28:05

Try this code...

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

I've had problems doing this, and ended up with this:

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

While this doesn't quite answer your question, try MBProgressHud, a third-party control that has this feature built-in. The examples supplied on Github should get you up to speed pretty quickly.

Try this code. Put YES for activity indicator and NO for 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];
}

Why not make use of the alerviewdelegate method

- (void)willPresentAlertView:(UIAlertView *)alertView

The advantage of this is we can see what size the alertview will actually be on screen, as iOS has precomputed this at this point, so no need for magic numbers - or overriding the class which Apple warn against !

And as of iOS7 I remember reading some document from Apple saying not to hard code any frame sizes but to always compute them from the app, or something along those lines ?

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

To pull reference to the loading bar in

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

Then use

UIProgressView *loadingBar = objc_getAssociatedObject(alertView, &myKey);

Remember to have defined

#import <objc/runtime.h>
static char myKey;

At the top of your class declaration

This is create a alert view

UIAlertController* alert=[UIAlertController alertControllerWithTitle:@"Message" message:@"This is test" preferredStyle:UIAlertControllerStyleAlert];

now add textfield

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField)
 {
     textField.placeholder=@"Enter Text label";
     [textField setBorderStyle:UITextBorderStyleRoundedRect];
     textField.backgroundColor=[UIColor whiteColor];
 }];

and added it on view

[self presentViewController:alert animated:YES completion:nil];

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!