Reputation: 3843
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
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];
Upvotes: 64
Views: 62624
Reputation: 35
let customActionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let firstButton = UIAlertAction(title: "First Button", style: .default, handler: { action in
//click action
})
firstButton.setValue(UIColor.black, forKey: "titleTextColor")
firstButton.setValue(UIColor.black, forKey: "imageTintColor")
firstButton.setValue(NSNumber(value: true), forKey: "checked")
let secondButton = UIAlertAction(title: "Second Button", style: .default, handler: { action in
//click action
})
secondButton.setValue(UIColor.black, forKey: "titleTextColor")
let cancelButton = UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
//cancel
})
cancelButton.setValue(UIColor.black, forKey: "titleTextColor")
customActionSheet.addAction(firstButton)
customActionSheet.addAction(secondButton)
customActionSheet.addAction(cancelButton)
present(customActionSheet, animated: true)enter code here
Upvotes: 0
Reputation: 15971
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)
Upvotes: 11
Reputation: 2272
You could add an image above the title label by subclassing UIAlertController
and adding \n
to the title string to make space for the UIImageView
. You'd have to compute the layout based on the font size. For images in the UIAlertAction
use KVC
like so self.setValue(image, forKey: "image")
. I would recommend to use an extension that checks for responds(to:)
. Here is sample implementation.
Upvotes: 13
Reputation: 6707
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
Upvotes: 124
Reputation: 4451
Swift Version:
actionBtn.setValue(UIImage.init(named: "completeDose"), forKey: "image")
Upvotes: 1
Reputation: 1202
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
Upvotes: 0
Reputation: 537
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];
Upvotes: 27
Reputation: 6384
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
Upvotes: 9