UITableViewHeaderFooterView: Unable to change background color

前端 未结 20 2095
误落风尘
误落风尘 2020-12-23 08:38

I\'m trying to change the background color of UITableViewHeaderFooterView. Although the view is appearing, the background color remains the default color. I\'m getting a log

相关标签:
20条回答
  • 2020-12-23 09:08

    Create UIView and set background color, then set it to self.backgroundView.

    - (void)setupBackgroundColor:(UIColor *) color {
        UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
        bgView.backgroundColor = color;
        self.backgroundView = bgView;
    }
    
    0 讨论(0)
  • 2020-12-23 09:11

    Make sure you set the backgroundColor of the contentView for your UITableViewHeaderFooterView:

    self.contentView.backgroundColor = [UIColor whiteColor];
    

    Then it will work.

    0 讨论(0)
  • 2020-12-23 09:11

    For a clear color, I use

    self.contentView.backgroundColor = [UIColor clearColor];
    self.backgroundView = [UIView new];
    self.backgroundView.backgroundColor = [UIColor clearColor];
    

    It seems fine to me.

    0 讨论(0)
  • 2020-12-23 09:12

    Setting the BackgroundView with clear color works fine for visible headers. If the table is scrolled to show headers at bottom ,this solution fails.

    P.S.My table consists only of headers without any cells.

    0 讨论(0)
  • 2020-12-23 09:13

    if you created a custom subclass of UITableViewHeaderFooterView with xib file then you should override setBackgroundColor. Keep it empty.

    -(void)setBackgroundColor:(UIColor *)backgroundColor {
    
    }
    

    And this will solve your problem.

    0 讨论(0)
  • 2020-12-23 09:14

    For me I tried everything stated above but still was getting the warning "Setting the background color on UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColor instead." then I tried this: within the xib file the background color for header view was selected to clear color instead of default once I changed it to default the warning went away.

    0 讨论(0)
提交回复
热议问题