Add Image to UIAlertAction in UIAlertController

2019-01-03 05:37发布

I have seen a couple screen shots of a UIAlertControllers with an image on the left of the row but I do not seen it in the documentation. An example visual is http://imgur.com/SISBvjB Here is the code that I have for my controller right now:

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条回答
Melony?
2楼-- · 2019-01-03 05:44

You could add image above title label by subclassing UIAlertController and adding \n to title string to make space for UIImageView. You'd have to compute layout based on font size. For images in UIAlertAction using KVC like so self.setValue(image, forKey: "image"). I would recommend extension that checks for responds(to:). Here is sample implementation.

enter image description here

查看更多
太酷不给撩
3楼-- · 2019-01-03 05:45

swift 3 extension , property and convenience 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
    }
}

thanks to Shahar Stern for the inspiration

查看更多
欢心
4楼-- · 2019-01-03 05:48

And that's how it's done:

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

the image property is not exposed, so there's no guarantee of this working in future releases, but works fine as of now

查看更多
一夜七次
5楼-- · 2019-01-03 05:50

Try something like this:

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

Tested this and it works for iOS 7.0

enter image description here

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-03 05:55

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)

查看更多
何必那么认真
7楼-- · 2019-01-03 05:55

Swift Version:

actionBtn.setValue(UIImage.init(named: "completeDose"), forKey: "image")
查看更多
登录 后发表回答