iOS UITableViewHeaderFooterView默认灰色问题

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ZEmptyTableViewHeaderFooterView : UITableViewHeaderFooterView

@property(nonatomic, strong) UIColor *bgColor;

@end

NS_ASSUME_NONNULL_END

#import "ZEmptyTableViewHeaderFooterView.h"

@implementation ZEmptyTableViewHeaderFooterView

-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if(self = [super initWithReuseIdentifier: reuseIdentifier]) {
        self.bgColor = [UIColor clearColor];
    }
    return self;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    //⚠️放在这里修改是因为在iOS12时,backgroundView并不会在初始化时创建
    self.backgroundView.backgroundColor = self.bgColor;
}

-(void)setBgColor:(UIColor *)bgColor {
    _bgColor = bgColor;
    self.backgroundColor = bgColor;
    self.contentView.backgroundColor = bgColor;
    self.backgroundView.backgroundColor = bgColor;
    if (@available(iOS 14.0, *)) {
        self.backgroundConfiguration = UIBackgroundConfiguration.clearConfiguration;
    } else {
        // Fallback on earlier versions
    }
}

注意:iOS 12以上,初始化时更改backgroundView的背景色有效,以下则无效,关键问题,测试时发现在初始化调用时,backgroundView视为nil,所以修改无效,因为我不清楚backgroundView创建时机,为确保更改有效,所以我暂时放在layoutSubviews中进行修改,这是因为在调用addSubview时,必然会调用layoutSubviews
如果有大神能确认backgroundView创建时机,望不吝告知,感谢!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容