【问题标题】:Can't change Background color of uitableviewheaderfooterview in IOS 7.0在 IOS 7.0 中无法更改 uitableviewheaderfooterview 的背景颜色
【发布时间】:2014-07-06 01:42:18
【问题描述】:

在 stackoverflow 中为 pre-iOS 7.0 甚至 ios 7.0 提供了number 的解决方案。但没有一个对我有用。

似乎大多数人都无法将背景颜色设置为透明。在我的情况下,我想去掉透明颜色,这样“星期天 4 号”就会得到一个纯色背景,并且在向下滚动一天中的时间时不会像屏幕截图中那样干扰标题。

我尝试设置 tint color 和 contentview 没有任何运气:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    FTDayHeaderView *header = [[self recordTableView] dequeueReusableHeaderFooterViewWithIdentifier:@"FTDayHeaderView"];
    //[header setTintColor:[UIColor greenColor]]; Didn't work either
    header.contentView.backgroundColor = [UIColor blackColor];
    return header;
}

这是FTDayHeaderView在IB中的设置:

我停用了opaque,没有任何效果。

更新: 根据要求。

#import "FTDayHeaderView.h"

@implementation FTDayHeaderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

【问题讨论】:

  • 你为什么不发表评论就投反对票?我做过研究,但对我没有用。努力提高生产力!!!
  • FTDayHeaderView 是 uiview 的子类吗?因此,您可以将 header.backgroundColor 设置为 blackColor,而不是 header.contentView.backgroundColor。也请分享 FTDayHeaderView 的 .m 文件内容。
  • FTDayHeaderViewUITableViewHeaderFooterView 的子类。我没有任何喜悦地尝试了你的伎俩。 [header setBackgroundColor:[UIColor greenColor]]; 更多详细信息,请参阅更新的问题。谢谢

标签: ios uitableview ios7


【解决方案1】:

不确定它是否能解决您的问题,但它解决了我的问题。尝试在willDisplayHeaderView 中设置颜色并根据您的代码进行调整。

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{    
    if ([view isMemberOfClass:[UITableViewHeaderFooterView class]]) {
        ((UITableViewHeaderFooterView *)view).backgroundView.backgroundColor = [UIColor whiteColor];
    }
}

【讨论】:

  • 这是正确答案。表格视图在其单元格和标题视图(包括selectedbackgroundColor)上设置基于状态的属性。此方法使委托有机会覆盖表视图决定的属性。同样,可以在willDisplayCell… 中覆盖单元格的背景颜色。
  • 不幸的是,问题一直存在于其他方面。正如 Rainer 所说,header.backgroundView 为零。
【解决方案2】:

试试这个:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeaderFooterView *sv = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"SomeViewId"];
    sv.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 48.0)];
    sv.backgroundView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    return sv;
}

我注意到,backgroundView 为 nil,并为背景颜色创建了一个视图。 (您可能需要更改视图的尺寸... 48 是我的标题视图的高度。)

【讨论】:

    猜你喜欢
    • 2013-03-14
    • 2014-09-13
    • 2021-06-07
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 2014-09-25
    相关资源
    最近更新 更多