UITableViewCell custom selected custom template is transparent

I have the following code that creates a UIView that I assign to the UITableViewCell property selectedBackgroundView . Everything works as expected, except for the background subview, which is transparent.

I use the same code to create a custom view that I assign a backgroundView , and it works fine.

What causes the transparency of the subview view for selectedBackgroundView , and how can I avoid this?

 - (UIView*) makeSelectedBackgroundView { // dimensions only for relative layout CGRect containerFrame = CGRectMake(0, 0, 320, 40); UIView* containerView = [[UIView alloc] initWithFrame:containerFrame]; containerView.autoresizesSubviews = YES; // dimensions only for relative layout CGRect subframe = CGRectMake(5, 5, 310, 30); UIView* subview = [[UIView alloc] initWithFrame:subframe]; subview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; subview.backgroundColor = [UIColor redColor]; subview.layer.cornerRadius = 5; subview.layer.borderWidth = 2; subview.layer.borderColor = [UIColor greenColor].CGColor; [containerView addSubview:subview]; return containerView; } 
+4
source share
5 answers

In the end, I ended up subclassing the UITableViewCell that contained the custom view object, and it worked.

-1
source

As the name ivar selectedBackgroundView shows, this background is displayed by the cell when it was selected.
I have to reload several methods ( - setSelected: animated: and - setHighlighted: animated:) of the UITableViewCell subclass to reset the background color of the subheadings to their values. See how UIKit do some magic in these template methods (iterating over all UIView subclasses and setting their background to clearColor )

+7
source

This code may be useful for you:

 UIImageView *cellImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height )]; cellImageView.contentMode = UIViewContentModeScaleAspectFit; // normal background view [cellImageView setImage:[UIImage imageNamed:@"*<ImageName>*"]]; [cell addSubview:cellImageView]; [cell sendSubviewToBack:cellImageView]; [cellImageView release], cellImageView = nil; 

Here cell is an object of a custom UITableViewCell .

You can also set the backgroundColor property.

0
source

I would try setting alpha for both containers and subView to 1.0

 [containerView setAlpha:1.0]; ... [subview setAlpha:1.0]; 

this should make your controls completely opaque.

You can also create several images for the background and use these images in the state of creating 2 views. Suppose you create 2 images (normalBackground.png and selectedBackground.png), and then set this image as the background of the cell. Here is a good tutorial.

0
source

Try setOpaque:YES in your views.

-1
source

Source: https://habr.com/ru/post/1380070/


All Articles