如何将图像添加到UIAlertView?

时间:2016-06-20 05:11:49

标签: ios objective-c uialertview

我想在UIAlertView的左上角添加图片。

UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)];

NSString *path = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"cloud.png"]];
UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path];
[imageView setImage:bkgImg];


[successAlert addSubview:imageView];

[successAlert show];

我正在使用此代码但是图像没有提供UIAlertView。请帮帮我

3 个答案:

答案 0 :(得分:1)

适用于iOS 7.0 +

使用:

[successAlert setValue:imageView forKey:@"accessoryView"];

答案 1 :(得分:1)

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];

答案 2 :(得分:1)

首先将您的图片添加到图片资源,并将名称命名为imgCloud然后将您的代码更新为:

UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

UIImageView *imageViewCloud = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)];
UIImage *bkgImg = [UIImage imageNamed:@"imgCloud"];
[imageViewCloud setImage:bkgImg];
[successAlert imageViewCloud forKey:@"accessoryView"];
[successAlert show];