我已经看过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];
答案 0 :(得分:85)
以及它是如何完成的:
let image = UIImage(named: "myImage")
var action = UIAlertAction(title: "title", style: .default, handler: nil)
action.setValue(image, forKey: "image")
alert.addAction(action)
图片属性未公开,因此在将来的版本中无法保证这一点,但截至目前工作正常
答案 1 :(得分:24)
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];
答案 2 :(得分: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];
经过测试,适用于iOS 7.0
答案 3 :(得分:5)
快速
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)
答案 4 :(得分:0)
Swift版本:
actionBtn.setValue(UIImage.init(named: "completeDose"), forKey: "image")
答案 5 :(得分:-1)
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的灵感