Chu*_*hun 111 iphone xcode uitableview ios
我正在尝试更改UITableViewHeaderFooterView的背景颜色.虽然视图正在显示,但背景颜色仍然是默认颜色.我从xcode获取日志说:
不推荐在UITableViewHeaderFooterView上设置背景颜色.请改用contentView.backgroundColor.
但是,以下选项均不起作用:
myTableViewHeaderFooterView.contentView.backgroundColor = [UIColor blackColor];
myTableViewHeaderFooterView.backgroundView.backgroundColor = [UIColor blackColor];
myTableViewHeaderFooterView.backgroundColor = [UIColor blackColor];
Run Code Online (Sandbox Code Playgroud)
我也尝试在xib文件中更改视图的背景颜色.
有什么建议?谢谢.
Swi*_*ect 182
设置任何颜色(使用任何alpha)的唯一方法是使用backgroundView:
迅速
self.backgroundView = UIView(frame: self.bounds)
self.backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
Run Code Online (Sandbox Code Playgroud)
OBJ-C
self.backgroundView = ({
UIView * view = [[UIView alloc] initWithFrame:self.bounds];
view.backgroundColor = [UIColor colorWithWhite: 0.5 alpha:0.5];
view;
});
Run Code Online (Sandbox Code Playgroud)
回应评论
这些其他选项都不可靠地工作(尽管下面的评论)
// self.contentView.backgroundColor = [UIColor clearColor];
// self.backgroundColor = [UIColor clearColor];
// self.tintColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)该backgroundView会自动调整大小.(无需添加约束)
使用UIColor(white: 0.5, alpha: 0.5)或控制alpha backgroundView.alpha = 0.5.
(当然,任何颜色都可以)
使用XIB时,请创建根视图a UITableViewHeaderFooterView并以backgroundView编程方式关联:
注册:
tableView.register(UINib(nibName: "View", bundle: nil),
forHeaderFooterViewReuseIdentifier: "header")
Run Code Online (Sandbox Code Playgroud)
加载:
override func tableView(_ tableView: UITableView,
viewForHeaderInSection section: Int) -> UIView? {
if let header =
tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") {
let backgroundView = UIView(frame: header.bounds)
backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
header.backgroundView = backgroundView
return header
}
return nil
}
Run Code Online (Sandbox Code Playgroud)►在GitHub上找到此解决方案以及有关Swift Recipes的其他详细信息.
Mat*_*att 80
您应该使用myTableViewHeaderFooterView.tintColor,或者为myTableViewHeaderFooterView.backgroundView指定自定义背景视图.
sas*_*ash 21
在iOS 7中contentView.backgroundColor为我工作,tintColor没有.
headerView.contentView.backgroundColor = [UIColor blackColor];
Run Code Online (Sandbox Code Playgroud)
虽然clearColor对我不起作用,我找到的解决方案是将backgroundView属性设置 为透明图像.也许它会帮助某人:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
headerView.backgroundView = [[UIImageView alloc] initWithImage:blank];
Run Code Online (Sandbox Code Playgroud)
Rud*_*f J 13
请确保您设置的的的backgroundColor contentView您UITableViewHeaderFooterView:
self.contentView.backgroundColor = [UIColor whiteColor];
Run Code Online (Sandbox Code Playgroud)
然后它会工作.
AmJ*_*mJa 11
对我来说,我尝试了上述所有内容,但仍然收到警告"在UITableViewHeaderFooterView上设置背景颜色已被弃用.请改用contentView.backgroundColor." 然后我尝试了这个:在xib文件中,标题视图的背景颜色被选中以清除颜色而不是默认颜色,一旦我将其更改为默认警告就消失了.

Ene*_*nso 11
对于纯色背景,设置contentView.backgroundColor应该就足够了:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let headerView = view as? UITableViewHeaderFooterView {
headerView.contentView.backgroundColor = .red // Works!
}
}
Run Code Online (Sandbox Code Playgroud)
对于具有透明度的颜色,包括.clear颜色,这不再有效:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let headerView = view as? UITableViewHeaderFooterView {
headerView.contentView.backgroundColor = .clear // Does not work
}
}
Run Code Online (Sandbox Code Playgroud)
对于完全透明的部分标题,将backgroundView属性设置为空视图:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let headerView = view as? UITableViewHeaderFooterView {
headerView.backgroundView = UIView() // Works!
}
}
Run Code Online (Sandbox Code Playgroud)
但是,请注意可能的副作用。除非 table view 设置为“Grouped”,否则当向下滚动时,节标题将在顶部对齐。如果部分标题是透明的,则单元格内容将被看到,这可能看起来不太好。
在这里,节标题具有透明背景:
为了防止这种情况,最好将部分标题的背景设置为与表视图或视图控制器的背景相匹配的纯色(或渐变)。
在这里,部分标题具有完全不透明的渐变背景:
在界面构建器中,在顶部元素上拉出您的 .xib,在属性检查器中将背景颜色设置为默认值。然后转到内容视图并在那里设置背景颜色(参考https://github.com/jiecao-fm/SwiftTheme/issues/24)。
为了清晰的颜色,我使用
self.contentView.backgroundColor = [UIColor clearColor];
self.backgroundView = [UIView new];
self.backgroundView.backgroundColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)
对我来说似乎很好.
| 归档时间: |
|
| 查看次数: |
45735 次 |
| 最近记录: |