在UIAlertController中将图像添加到UIAlertAction

use*_*052 52 ios uialertcontroller

我已经看过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];
Run Code Online (Sandbox Code Playgroud)

JP *_*sek 95

这就是它的完成方式:

let image = UIImage(named: "myImage")
var action = UIAlertAction(title: "title", style: .default, handler: nil)
action.setValue(image, forKey: "image")
alert.addAction(action)
Run Code Online (Sandbox Code Playgroud)

image属性没有公开,所以在将来的版本中无法保证这一点,但是现在工作正常

  • Apple不会拒绝它吗? (7认同)
  • @PatelJigar将渲染模式改为始终原始的`action.setValue(image.imageWithRenderingMode(.AlwaysOriginal),forKey:"image")` (5认同)
  • 谢谢!这是客观的C等价物:UIImage*image = [UIImage imageNamed:@"image.png"]; [bluetoothButton setValue:image forKey:@"image"]; (4认同)
  • 谢谢@JPHribovsek糟糕的SO用户.这个答案应该被接受为正确的答案.我能做的就是upvote (2认同)
  • 似乎不适用于 xCode 6.3 - 图标位置只有蓝色方块 (2认同)

小智 25

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];
Run Code Online (Sandbox Code Playgroud)


Pru*_*goe 10

尝试这样的事情:

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];
Run Code Online (Sandbox Code Playgroud)

经过测试,适用于iOS 7.0

在此输入图像描述

  • 它不起作用错误消息:setValue:forUndefinedKey:]:此类不是密钥配件兼容的键值编码. (7认同)
  • 不确定为什么你认为这是一个黑客,它都在框架中. (3认同)

str*_*ode 8

您可以通过子类化添加图像在标题标签上方,UIAlertController并添加\n到标题字符串以为其创建空间UIImageView.您必须根据字体大小计算布局.对于像这样UIAlertAction使用的图像.我建议使用检查的扩展名.这是示例实现.KVCself.setValue(image, forKey: "image")responds(to:)

在此输入图像描述


Har*_*kar 7

为了迅速

let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let action = UIAlertAction(title: NSLocalizedString("Share", comment: ""), style: .default, handler: { _ in
        })
let image = UIImage(named: "Temp1")
action.setValue(image?.withRenderingMode(.alwaysOriginal), forKey: "image")
actionSheet.addAction(action)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

注意:IMP Line withRenderingMode(.alwaysOriginal)