objective objective-c iphone uialertview

objective c - objective - ¿Es posible mostrar una imagen en UIAlertView?



uialertcontroller objective c (9)

Hice alguna modificación para la solución provista por Madhup.

La solución de Madhup funciona bien para mensajes cortos, sin embargo, cuando el mensaje es demasiado largo, el mensaje estará cubierto por la imagen.

Por lo tanto, agregué los siguientes pasos en el método de UIAlertViewDelegate - (void) willPresentAlertView: (UIAlertView *) alertView

  1. Agregue 8 "/ n" como prefijo de mensaje, para empujar el mensaje hacia abajo, reservando espacio para la imagen (mi imagen estaba restringida en 100x150)

  2. Detecte las subvistas de AlertView, para averiguar si existe UITextView.

    UITextView existirá solo cuando el mensaje sea demasiado largo.

  3. Si el UITextView no existe, todo irá bien, la imagen se muestra bien, el mensaje se muestra bien.

  4. Si existe UITextView, elimine el prefijo 8 "/ n" de UITextView.text y luego llame a UITextView.setFrame para cambiar el tamaño y cambiar la posición de la UITextview.

La acción anterior funciona bien.

Envío un NSDictionary como el mensaje que se muestra, el diccionario contiene 2 pares clave-valor, "msg" => cadena de mensaje real. "url" => como la imagen del sitio web.

Con el método sendSynchronousRequest de NSURLConnection, el código recuperará datos de imágenes de Internet en el camino.

- (void)showAlertView:(NSDictionary *)msgDic { NSLog(@"msgDic = %@", msgDic); NSMutableString *msg = [[NSMutableString alloc] initWithString:@"/n/n/n/n/n/n/n/n"]; if ([msgDic objectForKey:@"msg"]) { [msg appendFormat:@"%@", [msgDic objectForKey:@"msg"]]; } else { [msg setString:[msgDic objectForKey:@"msg"]]; } NSLog(@"msg = %@", msg); UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Alert Title" message:msg delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil]; if ([msgDic objectForKey:@"url"]) { NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[msgDic objectForKey:@"url"]]]; [request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData]; NSData *imgData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; if (imgData) { UIImage *shownImage = [UIImage imageWithData:imgData]; UIImageView *imgView = [[UIImageView alloc] initWithImage:shownImage]; [imgView setFrame:CGRectMake(floor(284-100)/2.0, 47, 100, 150)]; [alert addSubview:imgView]; [imgView release]; } } alert.delegate = self; [alert show]; [alert release]; [msgDic release]; } - (void)willPresentAlertView:(UIAlertView *)alertView { int viewCount = [alertView.subviews count]; NSLog(@"subviews count = %i", viewCount); if (viewCount > 0) { BOOL bFoundTextView = NO; for (int count=0; count<=[alertView.subviews count] -1; count++) { BOOL bIsTextView = NO; UIView *subView = [alertView.subviews objectAtIndex:count]; NSLog(@"view index %i classname = %@", count, [[subView class] description]); bIsTextView = [[[subView class] description] isEqualToString:@"UIAlertTextView"]; bFoundTextView |= bIsTextView; if (bIsTextView) { UITextView *textView = (UITextView *)subView; NSMutableString *msg = [[NSMutableString alloc] initWithString:textView.text]; [msg setString:[msg substringFromIndex:8]]; textView.text = msg; CGRect frame = textView.frame; if (frame.origin.y != 205) { frame.origin.y = 205; frame.size.height -= 155; [textView setFrame:frame]; } [msg release]; } } } }

¿Es posible agregar una imagen en un UIAlertView, como mostrar una imagen del archivo plist?


Nota para iOS 7 y superior

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { [alert setValue:imageView forKey:@"accessoryView"]; }else{ [alert addSubview:imageView]; }


Puedes hacerlo como:

UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 10, 40, 40)]; NSString *path = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"smile.png"]]; UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path]; [imageView setImage:bkgImg]; [successAlert addSubview:imageView]; [successAlert show];

Esto agregará una imagen en la esquina derecha de su vista de alerta, puede cambiar la imagen del marco para moverse.

Espero que esto ayude.



Usando pod de diseño puro:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Hello" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIImage *image = // target image here; CGSize size = image.size; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(leftMargin, topMargin, size.width, size.height)]; imageView.image = image; [alertController.view addSubview:imageView]; [imageView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:leftMargin]; [imageView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:topMargin]; [imageView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:rightMargin]; [imageView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:bottomMargin]; [imageView autoSetDimension:ALDimensionWidth toSize:size.width]; [imageView autoSetDimension:ALDimensionHeight toSize:size.height]; // add desired actions here [self presentViewController:alertController animated:YES completion:nil];


Versión rápida:

let alertView = UIAlertView(title: "Alert", message: "Alert + Image", delegate: nil, cancelButtonTitle: "OK") let imvImage = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) imvImage.contentMode = UIViewContentMode.Center imvImage.image = UIImage(named: "image_name") alertView.setValue(imvImage, forKey: "accessoryView") alertView.show()


en iOS 7 o superior, usa este código

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 282)]; UIImage *wonImage = [UIImage imageNamed:@"iberrys.png"]; imageView.contentMode=UIViewContentModeCenter; [imageView setImage:wonImage]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Arirang List" message:@"phiên bản: 1.0/n website: www.iberrys.com/n email: [email protected]/nmobile: 0918 956 456" delegate:self cancelButtonTitle:@"Đồng ý" otherButtonTitles: nil]; //check if os version is 7 or above if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { [alertView setValue:imageView forKey:@"accessoryView"]; }else{ [alertView addSubview:imageView]; } [alertView show];


UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:@"your Title" message:@"Your Message" delegate:nil cancelButtonTitle:@"Your Title" otherButtonTitles:nil]; UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 40, 40)]; NSString *loc = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Your Image Name"]]; UIImage *img = [[UIImage alloc] initWithContentsOfFile:loc]; [image setImage:img]; if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { [Alert setValue:image forKey:@"accessoryView"]; }else{ [Alert addSubview:image]; } [Alert show];


UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 282)]; UIImage *wonImage = [UIImage imageNamed:@"iberrys.png"]; imageView.contentMode = UIViewContentModeCenter; [imageView setImage:wonImage]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Arirang List" message:@"phiên bản: 1.0/n website: www.iberrys.com/n email: [email protected]/nmobile: 0918 956 456" delegate:self cancelButtonTitle:@"Đồng ý" otherButtonTitles:nil]; //check if os version is 7 or above if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { [alertView setValue:imageView forKey:@"accessoryView"]; } else { [alertView addSubview:imageView]; } [alertView show];