【问题标题】:Add Image to UIAlertAction in UIAlertController在 UIAlertController 中将图像添加到 UIAlertAction
【发布时间】:2014-12-08 10:29:01
【问题描述】:

我在行的左侧看到了几个 UIAlertControllers 的屏幕截图,但我在文档中没有看到它。一个示例视觉是 这是我现在的控制器代码:

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

【问题讨论】:

  • 没有 API 可以自定义 UIAlertController 的按钮。你看到的任何截图都没有使用UIAlertController
  • 也许我误解了 OP,但我认为他不想自定义警报按钮,而是在警报中添加图像。
  • 这就是我要创建的imgur.com/SISBvjB
  • @user1079052 您应该接受 JP 的回答作为正确答案。它绝对是 SO 上最好的一个

标签: ios uialertcontroller


【解决方案1】:

为了快速

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)

注意:IMP 线 withRenderingMode(.alwaysOriginal)

【讨论】:

    【解决方案2】:

    您可以通过将UIAlertController 子类化并将\n 添加到标题字符串来在标题标签上方添加图像,以便为UIImageView 腾出空间。您必须根据字体大小计算布局。对于UIAlertAction 中的图像,请使用KVC,如self.setValue(image, forKey: "image")。我建议使用检查responds(to:) 的扩展程序。 Here is sample implementation.

    【讨论】:

    • 太棒了,伙计。感谢您的解决方案。
    • 此解决方案代码不会为标题字符串上方的 UIImageView 腾出空间。见 - Inline Code Link, Inline Image Link
    【解决方案3】:

    这就是它的完成方式:

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

    图像属性未公开,因此无法保证此功能在未来版本中可以正常工作,但目前可以正常工作

    【讨论】:

    • 谢谢!这是目标 C 等价物: UIImage *image = [UIImage imageNamed:@"image.png"]; [bluetoothButton setValue:image forKey:@"image"];
    • 谢谢@JPHribovsek 可怜的 SO 用户。这个答案应该被接受为正确答案。我能做的就是点赞
    • 苹果不会拒绝吗?
    • 似乎不适用于 xCode 6.3 - 图标位置只有蓝色方块
    • @PatelJigar 将渲染模式更改为始终原始action.setValue(image.imageWithRenderingMode(.AlwaysOriginal), forKey: "image")
    【解决方案4】:

    Swift 版本:

    actionBtn.setValue(UIImage.init(named: "completeDose"), forKey: "image")
    

    【讨论】:

      【解决方案5】:

      swift 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 的启发

      【讨论】:

      • 您不应该将 NSManaged 用于非 NSManagedObjects 类。 UIAlertAction 在后台响应 setImage 的事实并不能证明这个 sn-p 防弹。如果“幕后”实现更改此代码将导致崩溃。
      • 任何使用未记录的api都应该谨慎使用,而且NSManager是swift的动态(不是synthesize的那个),你有不同的词来避免setter和getter的创建吗?
      • 我同意你关于使用无证 api 的观点。因此,如果您不想将图像参数与 KeyValue 方法一起使用,则必须创建一个自定义对象。就那么简单。 @NSManaged 用于核心数据对象。它的副作用是 getter 执行 valueForKey 而 setter 执行 setValue:forKey:。这段代码是绝对不安全的,如果以后图像值的实现发生变化,这肯定会导致崩溃。简短的回答是“你不应该这样做,就像你不应该使用私有 api 一样”
      【解决方案6】:
      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];
      

      【讨论】:

      • 嗨,我们如何将图像添加到文本的左侧。我的应用中有这个要求。谢谢
      • @SMS 在 Apple 将其公开为公共财产之前,它可能不像“想要移动图像”那么简单。您可以尝试继承 UIAlertController 并覆盖 viewWillLayoutSubviews 并通过 KVO 访问对象,如上面的 let image = self.valueForKey("image") 但这会给您图像。长话短说,要做到苹果不会拒绝您的应用程序可能非常困难。最简单的选择是创建自己的警报控制器
      • 这可以在视图的按钮(动作)上添加图像。无法将图像添加到消息中。
      • 这正是我所需要的。唯一的问题是你使用你使用的图像,虽然它有很多分辨率,但它在菜单中总是看起来很模糊。其他人有这个问题吗?
      【解决方案7】:

      试试这样的:

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

      对此进行了测试,它适用于 iOS 7.0

      【讨论】:

      • 感谢您的帮助,但我需要操作表中按钮左侧的图像。例如,当您在蓝牙、扬声器和设备等音频输出之间切换时,通话屏幕上会有一个......
      • 向 App Store 提交包含此“黑客”的应用是否安全?不会被拒吗?
      • 不知道为什么你认为这是一个 hack,它都在框架中。
      • 它不起作用错误消息:setValue:forUndefinedKey:]:这个类不符合键附件视图的键值编码。
      • 是否支持动画图像?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      • 2022-10-06
      相关资源
      最近更新 更多