Nick Curran
Nick Curran

Reputation: 484

UITableViewCell custom selectedBackgroundView background is transparent

I have the following code that creates a UIView that I assign to my UITableViewCell's selectedBackgroundView property. Everything works as expected, with the exception of the subview's background, which is transparent.

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

What is causing that subview to be transparent for selectedBackgroundView, and how can I avoid that?

- (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;
}

Upvotes: 4

Views: 4128

Answers (5)

Nick Curran
Nick Curran

Reputation: 484

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

Upvotes: -1

alinoz
alinoz

Reputation: 2822

I would try to set the alpha for both containerView and subView to 1.0

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

this should make your controls totally opaque.

You could also create some images for the background and use that images in state of creating 2 views. Let's say you create 2 image (normalBackground.png and selectedBackground.png) and then set this images as cell background. Here is a nice tutorial.

Upvotes: 0

Serhii Mamontov
Serhii Mamontov

Reputation: 4932

As we can see from name of ivar selectedBackgroundView, this background shown by cell when it was selected.
I've to reload few methods (– setSelected:animated: and – setHighlighted:animated:) of UITableViewCell subclass to reset background color of subviews back to their values. Look's like UIKit do some magic in this template methods (iterating over all UIView subclasses and set their background to clearColor)

Upvotes: 7

Mrunal
Mrunal

Reputation: 14128

This code might be helpful 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 custom UITableViewCell.

Also you can set backgroundColor property.

Upvotes: 0

Stavash
Stavash

Reputation: 14314

Try setOpaque:YES on your views.

Upvotes: -1

Related Questions