繁体   English   中英

UIButton不显示图像

[英]UIButton doesn't show image

我有一个UITableViewCell ,我要向其中添加五个UIButtons 我以编程方式创建按钮、添加操作和图像。 按钮放置在正确的位置,动作有效,标签已设置,但它们很清楚。 图片不显示。

这就是我用来创建其中一个按钮并将它们添加到单元格的方法,此代码位于tableView:cellForRowAtIndexPath:中:

if ([[self.compCompletion objectForKey:@"Key" isEqualToString:@"YES"]) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(11, 6, 21, 21);
            button.tag = 1;
            [button addTarget:self action:@selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside];
            [button setImage:[UIImage imageNamed:@"Comp-Completed.png"] forState:UIControlStateNormal];
            [button setContentMode:UIViewContentModeScaleToFill];
            [cell.contentView addSubview:button];
        }
        else {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(11, 6, 21, 21);
            button.tag = 1;
            [button addTarget:self action:@selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside];
            [button setImage:[UIImage imageNamed:@"Comp-Uncompleted.png"] forState:UIControlStateNormal];
            [button setContentMode:UIViewContentModeScaleToFill];
            [cell.contentView addSubview:button];
        }

我试过添加: [button setEnabled:YES]; , [button setHidden:NO]; , 和[button setNeedsDislpay]; 到没有运气的过程。 如何获得显示图像的按钮?

编辑:我的图像存在,创建了UIImage ,我可以将UIImageUIButton中拉出来并将其保存到文件中。 我已经检查了控制外观的UIButton的每个属性并更改了它们。 我还尝试了UIButtonTypeRoundedRectUIButtonTypeAddContact类型的空UIButton 我现在相信这与UITableViewCell不喜欢UIButtons或我如何将UIButton添加到UITableViewCell有关。

确保在复制文件时已将项目检查为目标成员资格。 它帮助我解决了问题。

我的错误是将图像拖到作为 pod 一部分的 images.xcassets 文件中,而不是主项目。 它在故事板中显示良好,但没有出现在应用程序中。

如果有人仍在寻找答案:一旦图像位于 assets.xcassets 中,那么我需要将它包含在目标构建阶段部分的“复制包资源”区域中。 然后我还必须设置一个透明的颜色,这样它就不会被遮挡。

项目->目标->构建阶段->复制捆绑资源->“+”->“添加其他”->在查找器中选择图像。

我知道这是一篇旧帖子,但我遇到了同样的问题,这是由于isHiddenisUserInteractionEnabled设置为隐藏按钮的值。

将它们设置为YESNO时必须小心。

如果有人正在寻找答案,请仔细检查您是否已将图像复制到项目目录中的 Assets.xcassets 而不是 pods 目录中,其次我在 Assets.xcassets 图像属性中使用渲染作为原始图像

它肯定能找到图像吗? 他们是否包含在项目中?

只是为了测试,尝试使用您已经在其他地方使用过的图像而不是 Comp-Completed.png - 如果有效,您就知道问题在于它无法找到该文件。

检查您的图像是否存在并已导入。 在我看来,除了图像名称之外,代码是相同的。 更干净一点:

UIImage *buttonImage;
if ([[self.compCompletion objectForKey:@"Key" isEqualToString:@"YES"]) {
    buttonImage = [UIImage imageNamed:@"Comp-Completed.png"];
} else {
    buttonImage = [UIImage imageNamed:@"Comp-Uncompleted.png"];
}

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(11, 6, 21, 21);
button.tag = 1;
[button addTarget:self action:@selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:buttonImage] forState:UIControlStateNormal];
[button setContentMode:UIViewContentModeScaleToFill];
[cell.contentView addSubview:button];
UIImage * normalImage = [UIImage imageNamed:@"Comp-Uncompleted.png"];
UIImage * selectedImage = [UIImage imageNamed:@"Comp-Completed.png"];

UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
button.frame = CGRectMake(11, 6, 21, 21);
button.tag = 1;
[button addTarget:self action:@selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:normalImage forState:UIControlStateNormal];
[button setImage:selectedImage forState:UIControlStateSelected];
[cell.contentView addSubview:button];

if ([[self.compCompletion objectForKey:@"Key" isEqualToString:@"YES"]) {
    button.selected = YES;
} else {
    button.selected = NO;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM