項(xiàng)目中需要修改UITableViewHeaderFooterView的背景顏色,在子類中我使用
self.backgroundColor = [UIColor redColor];
然而并沒(méi)有什么反應(yīng),Xcode提示如下
Setting the background color on UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColor instead.
這讓我看到希望立馬使用
self.contentView.backgroundColor = [UIColor redColor];
然而依舊沒(méi)什么卵用... 我的內(nèi)心是崩潰的,本著蘋(píng)果虐我千萬(wàn)遍,我待蘋(píng)果如初戀的原則,繼續(xù)google之。 終于找到解決辦法。
正確做法是使用backgroundView。
Swift
self.backgroundView = UIView(frame: self.bounds)
self.backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
Obj-C
self.backgroundView = ({
UIView * view = [[UIView alloc] initWithFrame:self.bounds];
view.backgroundColor = [UIColor colorWithWhite: 0.5 alpha:0.5];
view;
});