如何在Swift 3中使这个UITableView
及其单元格透明。
我已经查看了之前的帖子,但仍然得到了白色背景。
从我的代码中可以看出,我已经尝试过提到的各种方法:
override func viewDidLoad() {
self.communitiesTableView.delegate = self
self.communitiesTableView.dataSource = self
let background = CAGradientLayer().bespokeColor()
background.frame = self.view.bounds
// view.addSubview(background)
super.viewDidLoad()
// Do any additional setup after loading the view.
}
并且在我的单元格表函数中:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let title = self.communities[indexPath.row]
let cell = UITableViewCell()
cell.textLabel?.text = title
cell.textLabel?.font = UIFont(name: "Avenir", size: 12)
cell.textLabel?.textColor = UIColor.red
cell.textLabel?.backgroundColor = UIColor.clear
cell.contentView.backgroundColor = UIColor.clear
communitiesTableView.backgroundColor = UIColor.clear
cell.layer.backgroundColor = UIColor.clear.cgColor
return cell
}
这个 GIF 展示了问题-请注意黑色线条显示表格存在但没有填充(因为没有人登录)。
但是瞬间它变得清晰,然后变成白色。我哪里错了?
这是我找到的另一篇文章中的内容:
苹果文档说:
......在 iOS 7 中,默认情况下,单元格具有白色背景;在早期版本的 iOS 中,单元格继承封装表视图的背景颜色。如果您想更改单元格的背景颜色,请在您的表视图委托的 tableView:willDisplayCell:forRowAtIndexPath: 方法中进行更改。
您可能需要使用 willDisplayCell UITableView 委托方法来使您的表视图具有透明背景。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
我该如何应用上面的代码,因为它说适用于iOS 7?
cell.textLabel.backgroundColor = UIColor.clear
。如果问题仍然存在,可能有另一个视图的参与。此时,请在Storyboard中显示如何堆叠视图和单元格。这里不需要cell.isOpaque = false
,视图透明度与您的问题无关。 - shallowThoughtself.communitiesTableView.backgroundView = nil
,请移除它。 - Nirav D