【问题标题】:UIButton in UITableViewCell set background color not workingUITableViewCell 中的 UIButton 设置背景颜色不起作用
【发布时间】:2014-07-20 00:31:43
【问题描述】:

我有这样的自定义 UITableViewCell:

@property (weak) IBOutlet UIDefaultServerButton * defaultButton;
@property (weak) IBOutlet UILabel * nameLabel;
@property (weak) IBOutlet UIRoundButton * deleteButton;
@property (weak) IBOutlet NSLayoutConstraint * buttonHeight;
@property (weak) IBOutlet NSLayoutConstraint * buttonWidth;

@property id <TableViewCellDelegate> delegate;

- (void) setDefaultServer: (BOOL) def;
- (void) animateLock;
- (void) animateTrash;

在 cellForIndex.. 我添加 LongPress 识别器。长按时调用函数 animateLock。

- (void) animateLock
{

    [self.deleteButton setImage:[UIImage imageNamed:@"lock"] forState:UIControlStateNormal];

    self.deleteButton.hidden = NO;

    self.deleteButton.backgroundColor = [UIColor redColor];

}

奇怪的是按钮变得可见并且图像已更改,但 backgroundColor 被忽略(无论我设置哪种颜色)。

为什么?

【问题讨论】:

  • 您的 nameLabel 是否有覆盖它的背景颜色集?否则,请检查以确保 cell.contentView 背景颜色也是清晰的颜色。
  • No 在情节提要中,我将按钮设为蓝色并保持蓝色,因此它没有被任何东西覆盖。如果我在 init 方法中将背景颜色设置为红色,它会显示为红色,但只有在此方法中它完全忽略颜色设置。
  • 如果你为按钮添加图像,那么它如何显示背景颜色?
  • 图像几乎都是透明的。

标签: ios uitableview uibutton background-color


【解决方案1】:

您使用的代码非常好,但是背景颜色不可见的原因是背景图像隐藏了 UIButton 的背景颜色。您可以通过评论这一行来确认这一点

[self.deleteButton setImage:[UIImage imageNamed:@"lock"] forState:UIControlStateNormal];

然后按钮的背景将可见。

如果你能详细说明为什么要设置按钮的红色背景,我会调查一下。

【讨论】:

  • 查看以上所有推荐
  • 我也是,但没有
【解决方案2】:

您拍摄的图像可能与您的按钮大小相同。缩小图片或增加按钮的面积。

【讨论】:

  • 不,不可能。请看上面的评论。图像 90% 透明,(就像点击栏中的图标 - 周围的轮廓是透明的)
猜你喜欢
  • 2013-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-17
  • 1970-01-01
  • 2017-12-20
相关资源
最近更新 更多