iosobjective-cuicollectionviewcelluicollectionreusableviewinitwithframe

UICollectionViewCell -- make selectedBackgroundView bigger than cell itself


I am trying to figure out a way to make my selectedBackgroundView of a cell, larger than the cell itself.

My following approach aims to make the selectedBackgroundView: the cell's superview width (the entire width of the screen) X the cell's height

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

When this cell is selected, the background color only extends within the cell's limits (not up to the superview's height)

How can I best accomplish making the selectedBackgroundView larger than the cell? Am I taking the complete wrong approach here?


Solution

  • as I tested, selectedBackgroundView's frame is constant, your bg's width change does not work.

    - (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;
    }