#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];
//??放在這里修改是因?yàn)樵趇OS12時(shí),backgroundView并不會(huì)在初始化時(shí)創(chuàng)建
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
以上,初始化時(shí)
更改backgroundView
的背景色有效,以下則無(wú)效,關(guān)鍵問(wèn)題,測(cè)試時(shí)發(fā)現(xiàn)在初始化調(diào)用時(shí),backgroundView
視為nil
,所以修改無(wú)效,因?yàn)槲也磺宄?code>backgroundView的創(chuàng)建時(shí)機(jī)
,為確保更改有效,所以我暫時(shí)放在layoutSubviews
中進(jìn)行修改,這是因?yàn)樵谡{(diào)用addSubview
時(shí),必然會(huì)調(diào)用layoutSubviews
。
如果有大神能確認(rèn)backgroundView
的創(chuàng)建時(shí)機(jī)
,望不吝告知,感謝!!!