7

我的 TableView 的标题在 iOS13 中显示不好。不管我放什么颜色,它现在总是显示为浅灰色......

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{   //Section color & style

    UITableViewHeaderFooterView *v = (UITableViewHeaderFooterView *)view;

    v.backgroundView.alpha = 1;
    v.textLabel.textColor = sectionColor;
    v.textLabel.font = sectionFont;
    v.textLabel.numberOfLines = 1;
    v.textLabel.minimumScaleFactor = 0.5;
    v.textLabel.adjustsFontSizeToFitWidth = YES;
    v.backgroundView.backgroundColor = [UIColor blueColor];
} 

iOS12:
iOS12

iOS13: iOS13

这很奇怪,因为当我一步一步停止调试器时,它会在 iOS13 中向我显示良好的图像,但在应用程序中却没有:

一步步 任何建议,提前谢谢?

4

3 回答 3

8

我在我的一个应用程序中注意到了同样的事情。然后在控制台看到一条日志消息:

在 UITableViewHeaderFooterView 上设置背景颜色已被弃用。请改为将具有所需背景颜色的自定义 UIView 设置为 backgroundView 属性。

设置UIView具有所需背景颜色的自定义作为backgroundView解决UITableViewHeaderFooterView问题的方法。

代码示例

class SomeHeaderView: UITableViewHeaderFooterView {

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        configure()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func configure() {
        let backgroundView = UIView(frame: .zero)
        backgroundView.backgroundColor = .blue
        self.backgroundView = backgroundView
    }
}
于 2019-09-10T10:21:36.577 回答
8

这对我有用。

v.contentView.backgroundColor = .blue

代替

v.backgroundView.backgroundColor = .blue
于 2019-09-23T17:18:06.837 回答
0

尝试添加覆盖视图并为此视图更改此颜色。

UIView *coloredView = [[UIView alloc] init];
coloredView.backgroundColor = [UIColor blueColor];
[v addSubview:coloredView];

[[coloredView.leadingAnchor constraintEqualToAnchor:v.leadingAnchor constant:0] setActive:YES];
[[coloredView.trailingAnchor constraintEqualToAnchor:v.trailingAnchor constant:0] setActive:YES];
[[coloredView.topAnchor constraintEqualToAnchor:v.topAnchor constant:0] setActive:YES];
[[coloredView.bottomAnchor constraintEqualToAnchor:v.bottomAnchor constant:0] setActive:YES];
于 2019-09-09T13:00:33.467 回答