samedi 25 avril 2015

UITableViewHeaderFooterView can't change background color to clearColor


This is my TableHeaderView class, that extends UITableViewHeaderFooterView. This class does not have a Nib file and everything is done programmatically.

I want its background color to be transparent, but I cannot make it work. I've tried setting [self.backgroundView setBackgroundColor:[UIColor clearColor]], [self setBackgroundView:nil], [self.contentView setBackgroundColor:[UIColor clearColor]], but the final result is always a black background.

I've already spent hours on this, and I really have no idea how to fix this. What do I need to do to get this working?

#import "TableHeaderView.h"

@interface TableHeaderView ()

@property (strong, nonatomic) UIView *background;
@property (strong, nonatomic) UILabel *text;

@end

@implementation TableHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
  self = [super initWithReuseIdentifier:reuseIdentifier];

  if (self.contentView) {
    self.background = [UIView new];
    [self.background setBackgroundColor:[UIColor colorWithWhite:0.0f alpha:0.5f]];
    [self.background setTranslatesAutoresizingMaskIntoConstraints:NO];

    self.text = [UILabel new];
    [self.text setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.text setText:@"Label"];
    [self.text setFont:[UIFont fontWithName:@"HelveticaNeue" size:15.0f]];
    [self.text setTextColor:[UIColor whiteColor]];
    [self.text setBackgroundColor:[UIColor redColor]];

    [self.background addSubview:self.text];
    [self.contentView addSubview:self.background];

    [self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO];

    [self.background addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[L]-10-|"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:@{@"L": self.text}]];

    [self.background addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[L]|"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:@{@"L": self.text}]];

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[V]-10-|"
                                                                             options:0
                                                                             metrics:nil
                                                                               views:@{@"V": self.background}]];

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[V]|"
                                                                             options:0
                                                                             metrics:nil
                                                                               views:@{@"V": self.background}]];

    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:@{@"contentView" : self.contentView}]];

    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView]|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:@{@"contentView" : self.contentView}]];

    [self setBackgroundView:[UIView new]];                          // I can't change this background color to
    [self.backgroundView setBackgroundColor:[UIColor whiteColor]];  // be transparent. It gets black instead.
  }

  return self;
}

@end


Aucun commentaire:

Enregistrer un commentaire