2016-06-20 215 views
1

我想在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。请帮我

回答

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

感谢您的帮助。现在通过使用此代码。图像出现在警报视图中,但在底部显示并从左向右延伸。 –

+0

更改imageview框架 –

1

对于iOS 7.0 +

用途:

[successAlert setValue:imageView forKey:@"accessoryView"]; 
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];