UITableViewCell custom selectedBackgroundView issue
Following figure gives the idea of a cell structure. The selected Background view will hide Content view
So you can give a try by setting background color of cell as black. Your custom cell looks something like this
self.backgroundColor = [UIColor blackColor]; // Gives black background for you
// Set selected background view
UIView *backgroundView = [[UIView alloc]initWithFrame:self.bounds];
backgroundView.layer.borderColor = [[UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:1]CGColor];
backgroundView.layer.borderWidth = 10.0f;
self.selectedBackgroundView = backgroundView;
[backgroundView release];
// Set the content view
CGRect frame = CGRectMake(self.bounds.origin.x+5, self.bounds.origin.y+5, self.bounds.size.width-10, self.bounds.size.height-10);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
self.imageView = imageView;
[imageView release];
self.imageView.contentMode = UIViewContentModeScaleAspectFill ;
self.imageView.clipsToBounds = YES;
[self.contentView addSubview:self.imageView];
Comments
-
pmk almost 2 years
So I'm facing an issue with setting a custum selectedBackgroundView inside my
UITableViewCell
.My cell has a
contentView
that basically is aUIView
(frame = 0,0,80,70) with a black background and anUIImageView
as a subview.The imageView's
contentMode = UIViewContentModeScaleAspectFit;
This looks something like this:
Now I set the selectedBackgroundView like this:
//set the custom selected color UIView *bgColorView = [[UIView alloc] init]; bgColorView.backgroundColor = MY_TINT_COLOR; CGColorRef darkColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha: 0.25].CGColor; CGColorRef lightColor = [self.view.backgroundColor colorWithAlphaComponent:0.0].CGColor; //setting some gradients here //should not be relevant for the question [cell setSelectedBackgroundView:bgColorView];
This results in something like this:
My question now is why the
selectedBackgroundView
hides the black part of thecontentView
?I have already tried initializing my
bgColorView
with a frame starting atx = 80
, but this does not change anything.Also I have tried to explictly set the
backgroundColor
of theimageView
to black, same result.What could cause this behaviour?
-
pmk about 11 yearsHm I was hoping not to need to subclass UITableViewCell. Anyway I will give it a try and tell you a if it was successfull. Thanks so far!
-
pmk about 11 yearsThank you for your helpfull answer! I did not know about the cell's view hierarchy. Setting the
contentView
to aUIImageView
solved the problem :) -
Mihai Erős almost 6 yearsThank you for your answer, helped me alot!
-
GeneCode over 4 yearsI dont get it. From the diagram, contentview is most top, so why would setting selectedbackgroundview cover the contentview?