ArisRS
ArisRS

Reputation: 1394

UITableViewCell change selectedBackgroundView alpha?

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.

Upvotes: 2

Views: 2624

Answers (2)

Naveen kumar
Naveen kumar

Reputation: 800

// 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?

Upvotes: 3

Puneet Sharma
Puneet Sharma

Reputation: 9484

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

Upvotes: 3

Related Questions