1

I have set the highlight color for selecting a cell, its pretty simple. However, when I set the alpha of my UIView it does not respond. I tried putting a label in the view and setting the alpha and it did not work either. Does anyone know why this is happening?

Here is the code I'm using for setting highlight color:

[cell setSelectionStyle:UITableViewCellSelectionStyleDefault];

UIView *highlightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
highlightView.backgroundColor = [UIColor clearColor];
UILabel *backLabel = [[UILabel alloc] initWithFrame:highlightView.frame];
backLabel.backgroundColor = [UIColor blueColor];
backLabel.alpha = 0.5f;
[highlightView addSubview:backLabel];
cell.selectedBackgroundView = highlightView;

All I really want is for the highlight color to have a transparency effect. Setting it as a straight color like blue or red is just really intense with my background image. Thanks for any suggestions and help!

MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73

2 Answers2

3

This ended up being the solution:

UIView *highlightView = [[UIView alloc] init];
highlightView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:1.0 alpha:.5];
cell.selectedBackgroundView = highlightView;

This only worked when I DID NOT include:

cell.selectionStyle = UITableViewCellSelectionStyleDefault;

or

cell.selectionStyle = UITableViewCellSelectionStyleNone;

which is odd since everything I've read says exactly what @Michal pointed out in his answer.

MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73
2

Don't forget to set the style first! (Since iOS 8):

cell.selectionStyle = UITableViewCellSelectionStyleDefault;

pre-iOS 8 this was the way to go:

cell.selectionStyle = UITableViewCellSelectionStyleNone;
Michal
  • 15,429
  • 10
  • 73
  • 104
  • I updated my question, I had that commented out because when I use it instead of having the blue color, there is no color. I'm testing on an iOS 7.1.1 device and iOS 8.4 device btw – MSU_Bulldog Sep 10 '15 at 15:31
  • Oh but the problem is, that you are trying to have "too complicated" selection view. You don't set the `rect` for the `selectedBackgroundView`, it does it for you and stretches it out. That's why you think there is nothing going on. It ignores the frame and subviews you are setting... – Michal Sep 10 '15 at 15:34
  • If you do `[[UIView alloc] init]` and then set its background color to red and then set this view as the background view, it will be all red - it sets the frame automatically. – Michal Sep 10 '15 at 15:36
  • I got rid of the UILabel and replaced the initWithFrame with just init and tried setting the alpha of the UIView to .5 and I am still getting the same result - no highlight. – MSU_Bulldog Sep 10 '15 at 15:39
  • And if you leave alpha to 1.0 it highlights? – Michal Sep 10 '15 at 15:39
  • if I remove the UITableViewSelectionStyleNone it highlights, removing the alpha or changing it to 1 does not have any effect. – MSU_Bulldog Sep 10 '15 at 15:40
  • What about trying this? `highlightView.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];` – Michal Sep 10 '15 at 15:43
  • Same result - no highlight. When I remove setting the alpha, there is still no highlight, so the problem is that the UIView is not showing. – MSU_Bulldog Sep 10 '15 at 15:48
  • I honestly don't know, it must be something else in your code that's not here...see: http://stackoverflow.com/questions/18823808/uitableviewcell-change-selectedbackgroundview-alpha I can't get my head around what else it could be... – Michal Sep 10 '15 at 15:51
  • 1
    That link helped lead me to a solution. Apparently there is a known problem with setting the alpha of subviews, and odd enough I had to remove the selectionStyle code for it to start working. – MSU_Bulldog Sep 10 '15 at 15:57
  • Well at least we have found a solution :) – Michal Sep 10 '15 at 15:59
  • Help much appreciated! – MSU_Bulldog Sep 10 '15 at 15:59