I’m developing a little iOS component and I have a problem with a semi-transparent view with subviews. This is my scenario:
– one view with a semi-transparent background using [UIColor colorWithRed:green:blue:alpha]
– a little UITableView, with alpha = 1.0, added as a subview to the semi-transparent view
– some other subviews
Everything works well but the problem raises when the UITableView is scrolled up or down, in fact the area of the semi-transparent view around the UITableView loses its transparency becoming darker than its original background color.
Here’s an image to explain the problem:
Look at the space with the two arrows…
Can anyone help me with this problem?
Thank you so much for your attention!
Update:
Some code:
_alertBg = [[UIView alloc] initWithFrame:CGRectZero];
_alertBg.backgroundColor = self.backgroundColor;
_alertBg.frame = CGRectMake((_bgView.frame.size.width - 240) / 2, (_bgView.frame.size.height - 260) / 2, 240, 260);
_alertBg.layer.cornerRadius = 8.0;
_alertBg.layer.borderWidth = 2.0;
_alertBg.layer.borderColor = self.borderColor.CGColor;
_alertBg.layer.shadowColor = [UIColor grayColor].CGColor;
_alertBg.layer.shadowOffset = CGSizeMake(0, 3);
_alertBg.layer.shadowOpacity = 0.8;
_alertBg.layer.masksToBounds = YES;
[_bgView addSubview:_alertBg];
_table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_table.frame = CGRectMake(10, _titleLabel.frame.origin.y + _titleLabel.frame.size.height + 12, _alertBg.frame.size.width - 20, 150);
_table.layer.cornerRadius = 6.0;
_table.layer.masksToBounds = YES;
_table.delegate = self;
_table.dataSource = self;
[_alertBg addSubview:_table];
From the code above, self.backgroundColor is something like [UIColor colorWithRed:0 green:0 blue:1 alpha:0.7]
I put the available code in a test project, and got the same problem as you have. Commenting out
_alertBg.layer.shadowOpacity = 0.5;fixes the issue for me. Maybe someone can clarify why is this an issue, I have limited experience with QuartzCore.EDIT: Okay, inched closer to the real reason. It seems that if you don’t have set the
_alertBg.layer.shadowPathproperty all kinds of crazy things happen when you scroll the table (my guess here is that the table scroll calls a redraw of the_alertBgand the shadow redrawing gets called in quick succession far too many times and you get those visual artifacts).Adding a
shadowPathfixes the problem, so the layer code for the_alertBgshould be as following:Just fix the
shadowPathto your liking and you should be ready to go.PS: On my Google-quest I found this excellent blog post about shadows, it might be of help.