【问题标题】:UIButton doesn't show imageUIButton 不显示图像
【发布时间】:2012-04-01 06:35:42
【问题描述】:

我有一个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 的方式有关。

【问题讨论】:

  • 您确定图像名称正确(包括大小写)并且图像文件包含在您的项目中吗?
  • 是的,图片名称是正确的,我在不同的 UITableView 中使用相同的图片作为附件视图
  • 您确定该单元格显示按钮吗?尝试将其设置为 bacgroundColor 以确保...

标签: ios uitableview cocoa-touch uibutton uikit


【解决方案1】:

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

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];

【讨论】:

  • 图像存在并被导入,我在不同的 UITableView 中使用相同的图像,它们在那里工作。 (但我没有将它们放在 UIButton 中。)
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        确保您在复制文件时已将您的项目检查为 Target Membership。它帮助我解决了问题。

        【讨论】:

          【解决方案6】:
          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;
          }
          

          【讨论】:

            【解决方案7】:

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

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

            【讨论】:

            • 我使用了不同的图像,但它仍然无法正常工作。我还仔细检查了图像名称,它们是正确的。
            猜你喜欢
            • 1970-01-01
            • 2016-03-19
            • 1970-01-01
            • 2023-03-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多