objective-c 是否可以在 UIAlertView 中显示图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2323557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to show an Image in UIAlertView?
提问by summer
Is it possible to add image in an UIAlertView, like showing an image from the plist file?
是否可以在 UIAlertView 中添加图像,例如显示 plist 文件中的图像?
回答by Madhup Singh Yadav
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.
希望这可以帮助。
回答by Quang Minh
in iOS 7 or higher, use this code
在 iOS 7 或更高版本中,使用此代码
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: [email protected]\nmobile: 0918 956 456"
delegate:self
cancelButtonTitle:@"??ng y"
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];
回答by Anbu.Karthik
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];
回答by Rob Napier
You'll need to subclass UIAlertView and rearrange its subviews a bit. There are several tutorials for this kind of stuff:
您需要继承 UIAlertView 并稍微重新排列其子视图。这种东西有几个教程:
- Custom UIAlertView(probably the most applicable to your problem)
- Custom UIAlertView w/ TableView(also very handy)
- 自定义 UIAlertView(可能最适合你的问题)
- 带有 TableView 的自定义 UIAlertView(也非常方便)
回答by user9235122
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: [email protected]\nmobile: 0918 956 456"
delegate:self
cancelButtonTitle:@"??ng y"
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];
回答by Dennies Chang
I did some modification for the solution provided by Madhup.
我对 Madhup 提供的解决方案做了一些修改。
The solution from Madhup work fine for short message, however, when the message is too long, message will be covered by image.
Madhup 的解决方案适用于短消息,但是,当消息太长时,消息将被图像覆盖。
Hence, I added the following steps in method of UIAlertViewDelegate - (void)willPresentAlertView:(UIAlertView *)alertView
因此,我在 UIAlertViewDelegate 的方法中添加了以下步骤 - (void)willPresentAlertView:(UIAlertView *)alertView
Add 8 "\n" as prefix of message, to push the message down, reserving space for image (my image was restricted in 100x150)
Detect the subviews of AlertView, to find out if UITextView exists.
UITextView will exist only when the message is too long.
If the UITextView does not exist, everything will be fine, image shown good, message shown good.
If the UITextView exists, remove the 8 prefix "\n" from UITextView.text, and then call UITextView.setFrame to resize and change the position of the UITextview.
添加8个“\n”作为消息前缀,将消息下推,为图像保留空间(我的图像限制在100x150)
检测AlertView的子视图,判断UITextView是否存在。
UITextView 只有当消息太长时才会存在。
如果 UITextView 不存在,一切都会好起来的,图像显示良好,消息显示良好。
如果UITextView存在,去掉UITextView.text的8个前缀“\n”,然后调用UITextView.setFrame来调整UITextView的大小和位置。
The above action works fine.
上述操作工作正常。
I send an NSDictionary as the message to be shown, the dictionary contains 2 key-value pairs, "msg"=>real message string. "url"=>as the image from web site.
我发送一个 NSDictionary 作为要显示的消息,字典包含 2 个键值对,“msg”=> 真实消息字符串。“url”=>作为来自网站的图像。
With the NSURLConnection sendSynchronousRequest method, the code will retrieve image data from internet on the way.
使用 NSURLConnection sendSynchronousRequest 方法,代码将在途中从 Internet 检索图像数据。
- (void)showAlertView:(NSDictionary *)msgDic {
NSLog(@"msgDic = %@", msgDic);
NSMutableString *msg = [[NSMutableString alloc] initWithString:@"\n\n\n\n\n\n\n\n"];
if ([msgDic objectForKey:@"msg"]) {
[msg appendFormat:@"%@", [msgDic objectForKey:@"msg"]];
}
else {
[msg setString:[msgDic objectForKey:@"msg"]];
}
NSLog(@"msg = %@", msg);
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Alert Title"
message:msg
delegate:self
cancelButtonTitle:@"Close" otherButtonTitles:nil];
if ([msgDic objectForKey:@"url"]) {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[msgDic objectForKey:@"url"]]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
NSData *imgData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if (imgData) {
UIImage *shownImage = [UIImage imageWithData:imgData];
UIImageView *imgView = [[UIImageView alloc] initWithImage:shownImage];
[imgView setFrame:CGRectMake(floor(284-100)/2.0, 47, 100, 150)];
[alert addSubview:imgView];
[imgView release];
}
}
alert.delegate = self;
[alert show];
[alert release];
[msgDic release];
}
- (void)willPresentAlertView:(UIAlertView *)alertView {
int viewCount = [alertView.subviews count];
NSLog(@"subviews count = %i", viewCount);
if (viewCount > 0) {
BOOL bFoundTextView = NO;
for (int count=0; count<=[alertView.subviews count] -1; count++) {
BOOL bIsTextView = NO;
UIView *subView = [alertView.subviews objectAtIndex:count];
NSLog(@"view index %i classname = %@", count, [[subView class] description]);
bIsTextView = [[[subView class] description] isEqualToString:@"UIAlertTextView"];
bFoundTextView |= bIsTextView;
if (bIsTextView) {
UITextView *textView = (UITextView *)subView;
NSMutableString *msg = [[NSMutableString alloc] initWithString:textView.text];
[msg setString:[msg substringFromIndex:8]];
textView.text = msg;
CGRect frame = textView.frame;
if (frame.origin.y != 205) {
frame.origin.y = 205;
frame.size.height -= 155;
[textView setFrame:frame];
}
[msg release];
}
}
}
}
回答by Ryan Heitner
Note for IOS 7 and above
IOS 7 及以上版本注意事项
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
[alert setValue:imageView forKey:@"accessoryView"];
}else{
[alert addSubview:imageView];
}
回答by Nguy?n Ng?c B?n
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()
回答by user1232690
Using pure layout pod:
使用纯布局 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];

