UICollectionViewCell —使selectedBackgroundView大于单元格本身

问题描述 投票:0回答:2

[我正在尝试找出一种使单元格的selectedBackgroundView大于单元格本身的方法。

我的以下方法旨在制作selectedBackgroundView:单元格的超级视图宽度(屏幕的整个宽度)X单元格的高度

UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.superview.frame), CGRectGetHeight(cell.frame))];
bg.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = bg;

选择此单元格时,背景颜色仅在单元格的限制范围内延伸(不超过父视图的高度)

如何最好地使selectedBackgroundView大于单元格?我在这里采用完全错误的方法吗?

ios objective-c uicollectionviewcell uicollectionreusableview initwithframe
2个回答
2
投票

如我所测试的,selectedBackgroundView的帧是恒定的,您的bg的宽度变化不起作用。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell * cell = [tableView cellForRowAtIndexPath: indexPath];
    CGRect f = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
    f.size.height += 20;
    f.origin.y -= 10;
    UIView *bg = [[UIView alloc] initWithFrame: f];
    bg.backgroundColor = [UIColor redColor];
    [cell.contentView addSubview: bg];
    cell.contentView.clipsToBounds = false;
}

0
投票

@ dengApro的答案做到了。

f.size.width += 10;
UIView *bg = [[UIView alloc] initWithFrame: f];
bg.backgroundColor = [UIColor cellSelected];
[cell.superview addSubview: bg];
cell.superview.clipsToBounds = false;```
© www.soinside.com 2019 - 2024. All rights reserved.