如何在Swift 3中使UITableView透明?

19

如何在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,视图透明度与您的问题无关。 - shallowThought
在添加了其他人的建议后,问题仍然存在。我已经更新了代码以反映这一点。该表格包含在StackView中。如果我从此StackView中删除表格,则问题仍然存在... - RDowns
它清晰可见约一秒钟,然后就变成了白色。 - RDowns
问题可能出在这一行代码: self.communitiesTableView.backgroundView = nil,请移除它。 - Nirav D
Xcode 8.3.2 Swift 3。这让我很头疼。非常感谢您的发布。运行得很好。 - Cliff
显示剩余13条评论
13个回答

55

注意:以下代码已在Swift 3中进行测试。

方法1:

storyboard中选择您的tableViewCell并转到View下的Attributes Inspector,将Background更改为clear

方法2:尝试在cellForRowAt中使用以下代码

  cell.layer.backgroundColor = UIColor.clear.cgColor

输入图像描述

注意:如果以上方法不起作用,请尝试按下shift+option+command+k清除您的项目构建。

更新:请将下面的代码更新到cellForRowAt中...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
    cell.textLabel?.text = communities[indexPath.row]
    cell.textLabel?.font = UIFont(name: "Avenir", size: 12)
    cell.textLabel?.textColor = UIColor.red // set to any colour 
    cell.layer.backgroundColor = UIColor.clear.cgColor

    return cell
}

1
谢谢你所做的一切努力。它没有起作用 - 我认为你在这里展示的是正确的,而在这种情况下问题肯定与加载表格的方式有关。因为当它第一次出现时很清晰,然后就变成白色了。 - RDowns
1
我以为我已经尝试过这个了,直到我看了你的答案才意识到颜色设置为“默认”而不是“清除颜色”。 - Charles

15

您可以尝试这个

viewDidLoad 方法中:

tableView.backgroundColor = UIColor.clear

cellForRowAt:方法中:

cell.contentView.backgroundColor = UIColor.clear

对我来说有效。


4
这段内容加上 cell.backgroundColor = UIColor.clear 后对我起了作用。 - HH887
1
谢谢@HH887,只需使用cell.backgroundColor = UIColor.clear就可以了。 - vgarzom

10

对我来说前两个答案都不起作用,所以我在所有地方添加了一个清晰的背景,白色背景消失了。

cell.layer.backgroundColor = UIColor.clear.cgColor
cell.backgroundColor = .clear
tableView.layer.backgroundColor = UIColor.clear.cgColor
tableView.backgroundColor = .clear

SWIFT 5.5.3


8

您需要将表格和单元格背景都设置为透明。

在tableView函数内:

tableView.backgroundColor = .clear
cell.backgroundColor = .clear
tableView.tableFooterView = UIView()

仍然没有变化...奇怪。 - RDowns
刚刚添加了:tableView.tableFooterView = UIView(),但是还没成功。 - RDowns
很奇怪。我刚刚测试了一下,似乎对我来说运行良好。除了建议清理(Shift-Command-K)和重新启动之外,我真的无法提供更多帮助。 - antti-ka

5
为了使此功能正常工作,您需要实现一个新的tableview方法:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell,
               forRowAt indexPath: IndexPath) {
    cell.backgroundColor = UIColor.clear
}

2

您需要设置单元格的contentView的背景颜色。

cell.contentView.backgroundColor = UIColor.clear


1
也许你可以检查一下你的表格视图的透明度。

4
将alpha值设置为零将会隐藏整个表视图以及其所有子视图。Alpha值始终影响子视图的透明度。 - clemens
这是一个有用的答案。我有一个情况,我正在从各种核心数据实体加载数据到表格中。当表格从新实体加载值时,我希望表格刷新。但是,如果特定数据集中没有值,则不会发生这种情况- cellForRow不会被调用。在数据收集例程中将alpha设置为0,然后在cellForRow中将其设置为1可以解决我的表格重置问题。你的情况可能有所不同。 - ICL1901

1

仅设置这些是不够的,因为您尚未将clear color应用于tableViewcontentView,因此它在其中呈白色。在cellForRowAtIndexPath中尝试以下操作:

cell.contentView.backgroundColor = UIColor.clearColor()


1
有三个东西需要改变颜色。1)TableView的背景,2)TableView的单元格,3)TableView单元格的内容视图。只要您在单元格顶部创建任何其他内容,就可以实现完全透明的背景。 - Ahsan Ebrahim
一定还有我们忽略的东西 - 我已经更新了我的代码,以展示我已经做了你提到的所有事情(我相信)。 - RDowns
仅是一个快速的想法,您是否尝试清除tableView的颜色,但是将白色颜色设置为tableView后面的视图,并认为tableView的颜色未被清除?不确定,只是一个想法。也许这可以帮助您。@RDowns - Ahsan Ebrahim

1
TableViewTableViewCell的Storyboard中,将背景设置为“Clear/Default”而不是白色(或任何颜色)的透明度为0。

enter image description here

同时确保将ContentView的背景设置为透明,但是TableViewCell是我卡住的地方。这样做,您就不需要在代码中做任何事情。

1
我测试了使用Swift 3。它可以工作。
uiTableName.backgroundView = nil
uiTableName.backgroundColor = UIColor.clear

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接