2

I need to change alpha for the selectedBackgroundView of the UITableViewCell. So I do the following:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;

    UIView *bgView = [UIView new];
    bgView.backgroundColor = [UIColor redColor];
    bgView.alpha = 0.5;//This setting has no effect
    cell.selectedBackgroundView = bgView;
}

Cell selected in red color, but alpha settings has no effect for bgView.

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
ArisRS
  • 1,362
  • 2
  • 19
  • 41

2 Answers2

3

Try this:

[bgView setBackgroundColor:[[UIColor redColor] colorWithAlphaComponent:0.5]];

EDIT::
There is a known issue in providing alpha values on to the subviews. Look at this thread for better understanding:iOS controlling UIView alpha behaviour for subviews

Community
  • 1
  • 1
Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
3
// set selection color

UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:1 green:0.0 blue:0.0 alpha:0.5];
cell.selectedBackgroundView = myBackView;
[myBackView release];

Hope this link is useful for you.

Changing background color of selected cell?

UITableView Cell selected Color?

Community
  • 1
  • 1
Naveen kumar
  • 800
  • 1
  • 5
  • 23