将UIImage添加到UIAlertView无法正常工作

时间:2017-02-27 11:36:54

标签: ios objective-c xcode uiimage uialertview

我正在开发一款应用。我尝试使用下面的代码将UIIMage添加到UIAlertView,

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Instruction"
                                                    message:@"Please TAP on Screen to Continue."
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 50, 32, 32)];
UIImage *img = [UIImage imageNamed:@"pendingImg.png"];
[imageView setImage:img];
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    [alert setValue:imageView forKey:@"accessoryView"];
}else{
    [alert addSubview:imageView];
}
[alert show];

我的图片尺寸为32×32像素。我收到警告,如图所示,

Alert

我应该为此图片添加约束吗?或其他任何事情?

3 个答案:

答案 0 :(得分:2)

您可以将imageView contentMode设置为UIViewContentModeScaleAspectFit

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Instruction"
                                                    message:@"Please TAP on Screen to Continue."
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 50, 32, 32)];
UIImage *img = [UIImage imageNamed:@"pendingImg.png"];
 imageView.contentMode = UIViewContentModeScaleAspectFit;

[imageView setImage:img];
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    [alert setValue:imageView forKey:@"accessoryView"];
}else{
    [alert addSubview:imageView];
}
[alert show];

enter image description here

可能 Anbu.Karthik 建议的链接评论也对您有所帮助。

答案 1 :(得分:0)

试试这个

 UIAlertView* alert = [UIAlertView alloc] initWithTitle: @"Instruction" message: @"Please TAP on Screen to Continue." delegate:nil cancelButtonTitle:@"no" otherButtonTitles:@"yes", nil];

    UIImage* img = [UIImage imageNamed:@"pendingImg.png"];
    UIImageView* imgview = [UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
    [img setImage:imgMyImage];

    [alert setValue: imgview forKey:@"accessoryView"];
    [alert show];

答案 2 :(得分:0)

UIAlertView* alert = [[UIAlertView alloc] initWithTitle: @"Instruction" message: @"Please TAP on Screen to Continue." delegate:nil cancelButtonTitle:@"no" otherButtonTitles:@"yes", nil];

   UIImage* img = [UIImage imageNamed:@"add.png"];
    UIImageView* imgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
    [imgview setImage:img];
    [alert setValue: imgview forKey:@"accessoryView"];
    [alert show];
}