可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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];
回答1:
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
回答2:
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:
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

回答4:
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.

回答5:
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