【问题标题】:Add transparent overlay as subview of `uiimageview`添加透明覆盖作为`uiimageview`的子视图
【发布时间】:2018-05-16 04:55:42
【问题描述】:

我有一个带有自定义单元格的 UITable,每个单元格都有一个 uiimage。当我突出显示单元格时,图像没有突出显示。

为了突出显示单元格的图像,当我在表格中选择一个单元格时,会调用didHighlightRowAtIndexPath。在该方法中,我更改了单元格的背景颜色。我还调用了一个方法,indicatedImageViewPressedwhich I got from this post,为单元格的图像添加叠加层。

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    // Add your Colour.

    SpeciesCell* speciesCell = (SpeciesCell*)[tableView cellForRowAtIndexPath:indexPath];
    speciesCell.contentView.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
    speciesCell.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
    speciesCell.speciesImage.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
    [self indicatedImageViewPressed:speciesCell.imageView on:YES];
}

- (void)indicatedImageViewPressed:(UIImageView *)imageView on:(BOOL)on {
    UIView *overlay = [imageView viewWithTag:1]; // See if already created
    if (overlay == nil) {
        overlay = [[UIView alloc] initWithFrame:imageView.frame];
        overlay.tag = 1; // Mark view to find it next time
        overlay.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000]; // Your color overlay goes here
        [imageView addSubview:overlay];
        [imageView bringSubviewToFront:overlay];
    }
    overlay.hidden = !on;
}

但是,单元格的图像不会改变。不添加叠加层。我尝试bringSubviewToFront 以确保覆盖没有被其他视图遮挡,但它仍然不起作用。

更新:我可以为 speciesCell.speciesImage 添加边框宽度/颜色

【问题讨论】:

  • 仅出于调试目的,在 didHighlightRowAtIndexPath 方法中为 speciesCell.speciesImage 添加边框宽度和边框颜色红色,并让我知道它是否在图像上显示边框
  • @MuhammedIrfan 边框确实出现在 speciesCell.speciesImage 上!
  • 嗯,这是个问题
  • 你看到你的添加层代码没有问题
  • @Matt 在我看来,更改图像的alphaopacity 是一种更简单的方法,您不会遇到此问题。

标签: ios objective-c uitableview uiimageview addsubview


【解决方案1】:
speciesCell.contentView.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
speciesCell.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
speciesCell.speciesImage.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];

用下面的代码替换它:

 SpeciesCell.selectionStyle=UITableViewCellSelectionStyleGray;

【讨论】:

  • 但我不希望它是清晰的颜色,我想要一个浅灰色的透明覆盖层......
  • matt 第一次我没有得到你想要的覆盖层,试试这个绝对有效
  • 我认为他想在点击 uitableviewcell-@trungduc 时更改单元格的背景色
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
  • 2012-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多