在UIAlertController中将图像添加到UIAlertAction

我已经看到了一个UIAlertControllers的屏幕截图,在行的左边有一个图像,但是我没有在文档中看到它。 一个视觉的例子是 http://imgur.com/SISBvjB 下面是我现在用于控制器的代码:

UIAlertController * view = [UIAlertController alertControllerWithTitle:@"My Title" message:@"Select you Choice" preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }]; [view addAction:ok]; [self presentViewController:view animated:YES completion:nil]; 

这就是这样做的:

 let image = UIImage(named: "myImage") var action = UIAlertAction(title: "title", style: .Default, handler: nil) action.setValue(image, forKey: "image") alert.addAction(action) 

图片属性没有公开,所以在将来的版本中不能保证它的正常工作,但是现在可以正常工作

 UIAlertController * view= [UIAlertController alertControllerWithTitle:@"Staus ! " message:@"Select your current status" preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction* online = [UIAlertAction actionWithTitle:@"Online" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { //Do some thing here [view dismissViewControllerAnimated:YES completion:nil]; }]; UIAlertAction* offline = [UIAlertAction actionWithTitle:@"Offline" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [view dismissViewControllerAnimated:YES completion:nil]; }]; UIAlertAction* doNotDistrbe = [UIAlertAction actionWithTitle:@"Do not disturb" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [view dismissViewControllerAnimated:YES completion:nil]; }]; UIAlertAction* away = [UIAlertAction actionWithTitle:@"Do not disturb" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) { [view dismissViewControllerAnimated:YES completion:nil]; }]; [online setValue:[[UIImage imageNamed:@"online.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"]; [offline setValue:[[UIImage imageNamed:@"offline.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"]; [doNotDistrbe setValue:[[UIImage imageNamed:@"DND.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"]; [away setValue:[[UIImage imageNamed:@"away.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"]; [view addAction:online]; [view addAction:away]; [view addAction:offline]; [view addAction:doNotDistrbe]; [self presentViewController:view animated:YES completion:nil]; 

尝试这样的事情:

 UIAlertView* alert = [UIAlertView alloc] initWithTitle: @"Test Alert" message: @"Alert With Custom View" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil]; UIImage* imgMyImage = [UIImage imageNamed:@"myImage.png"]; UIImageView* ivMyImageView = [UIImageView alloc] initWithFrame:CGRectMake(0, 0, imgMyImage.size.width, imgMyImage.size.height)]; [ivMyImageView setImage:imgMyImage]; [alert setValue: ivMyImageView forKey:@"accessoryView"]; [alert show]; 

经过testing,它适用于iOS 7.0

在这里输入图像描述

您可以通过UIAlertController并在标题string中添加\n来为UIImageView创build空间,从而在标题标签上添加图像。 你必须根据字体大小来计算布局。 对于使用KVC UIAlertAction self.setValue(image, forKey: "image")self.setValue(image, forKey: "image") 。 我会build议扩展,检查responds(to:) 。 这里是示例实现。

在这里输入图像描述

迅速3扩展,财产和便利初始。

 extension UIAlertAction{ @NSManaged var image : UIImage? convenience init(title: String?, style: UIAlertActionStyle,image : UIImage?, handler: ((UIAlertAction) -> Swift.Void)? = nil ){ self.init(title: title, style: style, handler: handler) self.image = image } } 

感谢Shahar Stern的灵感