Is it possible to show an Image in UIAlertView?

后端 未结 9 426
别跟我提以往
别跟我提以往 2020-11-29 02:01

Is it possible to add image in an UIAlertView, like showing an image from the plist file?

相关标签:
9条回答
  • 2020-11-29 02:26
    UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:@"your Title" message:@"Your   Message" delegate:nil cancelButtonTitle:@"Your Title" otherButtonTitles:nil];
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 40, 40)];
    
    NSString *loc = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Your Image Name"]];
    UIImage *img = [[UIImage alloc] initWithContentsOfFile:loc];
    [image setImage:img];
    
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        [Alert setValue:image forKey:@"accessoryView"];
    }else{
        [Alert addSubview:image];
    }
    
    [Alert show];
    
    0 讨论(0)
  • 2020-11-29 02:28

    Swift version:

        let alertView = UIAlertView(title: "Alert", message: "Alert + Image", delegate: nil, cancelButtonTitle: "OK")
        let imvImage = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        imvImage.contentMode = UIViewContentMode.Center
        imvImage.image = UIImage(named: "image_name")
        alertView.setValue(imvImage, forKey: "accessoryView")
        alertView.show()
    
    0 讨论(0)
  • 2020-11-29 02:28

    Using pure layout pod:

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Hello"
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    UIImage *image = // target image here;
    CGSize size = image.size;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(leftMargin, topMargin, size.width, size.height)];
    imageView.image = image;
    [alertController.view addSubview:imageView];
    [imageView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:leftMargin];
    [imageView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:topMargin];
    [imageView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:rightMargin];
    [imageView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:bottomMargin];
    [imageView autoSetDimension:ALDimensionWidth toSize:size.width];
    [imageView autoSetDimension:ALDimensionHeight toSize:size.height];
    // add desired actions here
    [self presentViewController:alertController animated:YES completion:nil];
    
    0 讨论(0)
  • 2020-11-29 02:32

    Note for IOS 7 and above

    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        [alert setValue:imageView forKey:@"accessoryView"];
    }else{
        [alert addSubview:imageView];
    }
    
    0 讨论(0)
  • You can do it like:

    UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 10, 40, 40)];
    
        NSString *path = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"smile.png"]];
        UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path];
        [imageView setImage:bkgImg];
    
        [successAlert addSubview:imageView];
    
        [successAlert show];
    

    This will add a image in the right corner of your alertview you can change the frame image to move around.

    Hope this helps.

    0 讨论(0)
  • 2020-11-29 02:37

    in iOS 7 or higher, use this code

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 282)];
    UIImage *wonImage = [UIImage imageNamed:@"iberrys.png"];
    imageView.contentMode=UIViewContentModeCenter;
    [imageView setImage:wonImage];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Arirang List"
                                                         message:@"phiên bản: 1.0\n website: www.iberrys.com\n email: quangminh@berrys.com\nmobile: 0918 956 456"
                                                        delegate:self
                                               cancelButtonTitle:@"Đồng ý"
                                               otherButtonTitles: nil];
    //check if os version is 7 or above
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        [alertView setValue:imageView forKey:@"accessoryView"];
    }else{
        [alertView addSubview:imageView];
    }
     [alertView show];
    
    0 讨论(0)
提交回复
热议问题