Add Image to UIAlertAction in UIAlertController

匿名 (未验证) 提交于 2019-12-03 01:26:01

问题:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!