是否可以在UIAlertView中显示图像?

sum*_*mer 21 iphone objective-c uialertview

是否可以在UIAlertView中添加图像,比如显示plist文件中的图像?

Mad*_*dav 43

你可以这样做:

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];
Run Code Online (Sandbox Code Playgroud)

这将在警报视图的右上角添加图像,您可以更改框架图像以移动.

希望这可以帮助.

  • 对于从谷歌来这里的人来说,这在iOS7中不再适用.然而,您可以查看[this](https://github.com/wimagguc/ios-custom-alertview),它是具有图像支持的UIAlertView的副本. (8认同)

小智 10

在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: 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];
Run Code Online (Sandbox Code Playgroud)


Rob*_*ier 5

您需要将UIAlertView子类化并稍微重新排列其子视图.这种东西有几个教程:


Anb*_*hik 5

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];
Run Code Online (Sandbox Code Playgroud)