ios - UITableViewHeaderFooterView can't change background color to clearColor -
this tableheaderview
class, extends uitableviewheaderfooterview
. class not have nib file , done programmatically.
i want background color transparent, cannot make work. i've tried setting [self.backgroundview setbackgroundcolor:[uicolor clearcolor]]
, [self setbackgroundview:nil]
, [self.contentview setbackgroundcolor:[uicolor clearcolor]]
, final result black background.
i've spent hours on this, , have no idea how fix this. need 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]]; // can't change background color [self.backgroundview setbackgroundcolor:[uicolor whitecolor]]; // transparent. gets black instead. } return self; } @end
update
i tried using nib file , using initializer:
- (instancetype)initwithreuseidentifier:(nsstring *)reuseidentifier { self = [super initwithreuseidentifier:reuseidentifier]; if (self) { [self.contentview addsubview:[[[nsbundle mainbundle] loadnibnamed:@"tableheaderview" owner:self options:nil] firstobject]]; } return self; }
the nib file sets views old code. still doesn't work, can't transparent background.
update 2
the problem overriding drawrect:
, , doing caused black background. moving code inside method solved problem.
Comments
Post a Comment