How to add UIImageView in UIAlertController?

蹲街弑〆低调 提交于 2019-12-18 16:45:49

问题


I want UIAlertController to present an alert with UIImageView in an ActionSheet. But when I run the application it is terminating.

This is my code:

UIAlertController *alert = [UIAlertController
                                  alertControllerWithTitle:@"Title"
                                  message:@"Welcome"
                                  preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okButton = [UIAlertAction
                            actionWithTitle:OK
                            style:UIAlertActionStyleDefault
                            handler:^(UIAlertAction *action)
                            {
                            }];
[alert addAction:okButton];
UIImageView *imgv = [[UIImageView alloc]initWithFrame:CGRectMake(20,20,50,50)];
imgv.image = [UIImage imageNamed:@"kaga.jpg"];
[alert setValue:imgv forKey:@"image"];
[self presentViewController:alert animated:YES completion:nil];

回答1:


It is not possible to add an image to a UIAlertController according to Apple Doc.

if you want then you can create your own custom view like:

https://github.com/wimagguc/ios-custom-alertview

If you want to take image appear on button then Try like this:

UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Title"
                              message:@"Welcome"
                              preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction* okButton = [UIAlertAction actionWithTitle:@"OK"
                            style:UIAlertActionStyleCancel
                            handler:^(UIAlertAction * action) {
                              //Do some thing here
                            }];

[okButton setValue:[[UIImage imageNamed:@"kaga.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];

[alert addAction:okButton];
[self presentViewController:alert animated:YES completion:nil];



回答2:


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.

extension UIAlertAction {

    /// Image to display left of the action title
    var actionImage: UIImage? {
        get {
            if self.responds(to: Selector(Constants.imageKey)) {
                return self.value(forKey: Constants.imageKey) as? UIImage
            }
            return nil
        }
        set {
            if self.responds(to: Selector(Constants.imageKey)) {
                self.setValue(newValue, forKey: Constants.imageKey)
            }
        }
    }

    private struct Constants {
        static var imageKey = "image"
    }
}



回答3:


change this:

UIImageView *imgv=[[UIImageView alloc]initWithFrame:CGRectMake(20,20,50,50)];
    imgv.image=[UIImage imageNamed:@"kaga.jpg"];
    [alert setValue:imgv  forKey:@"image"];

to this:

UIImage *img= [UIImage imageNamed:@"kaga.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [alert setValue:img  forKey:@"image"];


来源:https://stackoverflow.com/questions/37848224/how-to-add-uiimageview-in-uialertcontroller

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