OGeek|极客世界-中国程序员成长平台

标题: ios - 将 UIImage 添加到 UIAlertView 未按预期工作 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 00:01
标题: ios - 将 UIImage 添加到 UIAlertView 未按预期工作

我正在开发一个应用程序。我尝试使用以下代码将 UIIMage 添加到 UIAlertView,

UIAlertView *alert = [[UIAlertView alloc] initWithTitle"Instruction"
                                                    message"lease 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

我应该为此图像添加约束吗?还是要做什么?



Best Answer-推荐答案


可以将imageView contentMode设置为UIViewContentModeScaleAspectFit,

UIAlertView *alert = [[UIAlertView alloc] initWithTitle"Instruction"
                                                    message"lease 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建议的链接评论对您也有帮助。

关于ios - 将 UIImage 添加到 UIAlertView 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42484813/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4