添加透明叠加层作为`uiimageview`的子视图

时间:2017-12-02 03:41:02

标签: ios objective-c uitableview uiimageview addsubview

我有一个带有自定义单元格的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

添加边框宽度/颜色

1 个答案:

答案 0 :(得分:0)

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;