How to add UIImageView in UIAlertController?
Solution 1
It is not possible to add an image to a UIAlertController
according to Apple Doc.
if you want then you can create your own custom view like:
https://github.com/wimagguc/ios-custom-alertview
If you want to take image appear on button
then Try like this:
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Title"
message:@"Welcome"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* okButton = [UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
//Do some thing here
}];
[okButton setValue:[[UIImage imageNamed:@"kaga.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[alert addAction:okButton];
[self presentViewController:alert animated:YES completion:nil];
Solution 2
You could add an image above the title label by subclassing UIAlertController
and adding \n
to the title string to make space for the UIImageView
. You'd have to compute the layout based on the font size. For images in the UIAlertAction
use KVC
like so: self.setValue(image, forKey: "image")
. I would recommend to use an extension that checks for responds(to:)
.
Here is sample implementation.
extension UIAlertAction {
/// Image to display left of the action title
var actionImage: UIImage? {
get {
if self.responds(to: Selector(Constants.imageKey)) {
return self.value(forKey: Constants.imageKey) as? UIImage
}
return nil
}
set {
if self.responds(to: Selector(Constants.imageKey)) {
self.setValue(newValue, forKey: Constants.imageKey)
}
}
}
private struct Constants {
static var imageKey = "image"
}
}
Yasuhiro Kondo
Updated on June 17, 2022Comments
-
Yasuhiro Kondo almost 2 years
I want
UIAlertController
to present an alert withUIImageView
in anActionSheet
. But when I run the application it is terminating.This is my code:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Welcome" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *okButton = [UIAlertAction actionWithTitle:OK style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { }]; [alert addAction:okButton]; UIImageView *imgv = [[UIImageView alloc]initWithFrame:CGRectMake(20,20,50,50)]; imgv.image = [UIImage imageNamed:@"kaga.jpg"]; [alert setValue:imgv forKey:@"image"]; [self presentViewController:alert animated:YES completion:nil];
-
Yasuhiro Kondo almost 8 yearsThanks but picture not appear
-
Suraj Sukale almost 8 yearsWhats the issue you facing now?
-
Yasuhiro Kondo almost 8 yearsI want use uialertcontroller show picture in alerts message box. but I use
[alert setValue:[[UIImage imageNamed:@"kaga.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
it showthis class is not key value coding-compliant for the key image.
-
Yasuhiro Kondo almost 8 yearsno It appear in ok button I want to show in message like i.stack.imgur.com/kuY0O.png
-
Suraj Sukale almost 8 yearsohh..now i got exactly what you want... wait for few minutes.. I will tell your solution..:)
-
Suraj Sukale almost 8 yearsSee my answer bro.. i hope it will provide some important information...@YasuhiroKondo
-
Suraj Sukale almost 8 yearsYes, all the best..:).. if its helpful then you can accept my answer:)
-
Lukas Würzburger about 5 years@jtth I'm not a C# developer, so I don't know. Sorry
-
Omair Ahmed about 4 yearsIs this acceptable since Apple docs don't allow subclassing. I'm assuming they don't allow extension either. But they do have this kind of alert when you Airdrop something.