首页 文章

在UIAlertController中将图像添加到UIAlertAction

提问于
浏览
46

我已经看过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];

7 回答

  • 23

    这就是它的完成方式:

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

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

  • 83
    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];
    
  • 3

    尝试这样的事情:

    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

    enter image description here

  • 9

    您可以通过继承 UIAlertController 并将 \n 添加到 Headers 字符串以在 UIImageView 中添加空格来在 Headers 标签上方添加图像 . 您必须根据字体大小计算布局 . 对于 UIAlertAction 中使用 KVC 的图像,如此 self.setValue(image, forKey: "image") . 我建议检查 responds(to:) 的扩展名 . Here is sample implementation.

    enter image description here

  • 0

    For swift

    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)
    

    Note: IMP Line withRenderingMode(.alwaysOriginal)

  • -2

    Swift版本:

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

    swift 3扩展,属性和便利init .

    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的灵感

相关问题