ios 如何在 Swift 3 中使这个 UITableView 清晰(透明)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40678482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 10:50:02  来源:igfitidea点击:

How do I make this UITableView clear (transparent) in Swift 3

iosswiftuitableviewswift3

提问by RDowns

How do I make this UITableViewand it's cells clear in Swift 3.

我如何UITableView在 Swift 3 中清除它和它的单元格。

I have gone through the previous threads but I am still getting a white background.

我已经完成了之前的线程,但我仍然得到白色背景。

As you can see from my code I have tried the various methods mentioned:

正如您从我的代码中看到的,我尝试了提到的各种方法:

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.
}

and in my cell table function:

在我的单元格表功能中:

  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

}

This gif shows the problem - notice the black lines showing the table is present just not populated (as no-one is logged in)

此 gif 显示了问题 - 请注意显示表格的黑线只是未填充(因为没有人登录)

But for a second it is clear, then turns white. Where am I going wrong?

但有一秒钟它是清楚的,然后变成白色。我哪里错了?

This is from another post that I have found:

这是我发现的另一篇文章:

Apple document says

苹果文件说

... In iOS 7, cells have a white background by default; in earlier versions of iOS, cells inherit the background color of the enclosing table view. If you want to change the background color of a cell, do so in the tableView:willDisplayCell:forRowAtIndexPath: method of your table view delegate.

... 在 iOS 7 中,单元格默认为白色背景;在早期版本的 iOS 中,单元格继承了封闭表格视图的背景颜色。如果要更改单元格的背景颜色,请在表视图委托的 tableView:willDisplayCell:forRowAtIndexPath: 方法中进行。

You might need to use willDisplayCell UITableView delegate method to have a transparent background for your table view .

您可能需要使用 willDisplayCell UITableView 委托方法为您的表格视图设置透明背景。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath

{
  [cell setBackgroundColor:[UIColor clearColor]];
}

How do I apply the code above as it says its for iOS 7?

我如何应用上面的代码,因为它适用于 iOS 7?

回答by Joe

Note:Below code been tested in Swift 3.

注意:以下代码已在Swift 3.

Method 1:

方法一:

Select your tableViewCell from your storyboardand goto Attributes Inspectorunder Viewchange Backgroundto clear

从您选择您tableViewCellstoryboard并转到Attributes InspectorView改变Backgroundclear

Method 2:Try below code inside your cellForRowAt

方法 2:在您的内部尝试以下代码cellForRowAt

  cell.layer.backgroundColor = UIColor.clear.cgColor

enter image description here

在此处输入图片说明

Note :If above method didn't works.try clear your project build by pressing shift + option + command + k

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

Update:Update your cellForRowAtfrom below code...

更新: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
}

回答by Eugene Lezov

You can try this

你可以试试这个

In viewDidLoad:

viewDidLoad 中

tableView.backgroundColor = UIColor.clear

In cellForRowAt:

cellForRowAt 中:

cell.contentView.backgroundColor = UIColor.clear

It's work for me.

这对我有用。

回答by antti-ka

You have to set both table and cell background clear.

您必须将表格和单元格背景都设置为清晰。

Inside tableView function:

在 tableView 函数中:

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

回答by El-Burritos

The two first answer doesn't work for me so I add a clear background everywhere and the white bg go away.

前两个答案对我不起作用,所以我在各处添加了清晰的背景,白色背景消失了。

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

SWIFT 5.1.2

斯威夫特 5.1.2

回答by Hiram Elguezabal

For this to work you have to implement a new tableview method:

为此,您必须实现一个新的 tableview 方法:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell,
               forRowAt indexPath: IndexPath) {
    cell.backgroundColor = UIColor.clear
}

回答by dlbuckley

You are missing setting the background colour of contentView of the cell.

您缺少设置单元格 contentView 的背景颜色。

cell.contentView.backgroundColor = UIColor.clear

cell.contentView.backgroundColor = UIColor.clear

回答by Cory Billeaud

Make sure in the Identity Inspector you have Custom Class -> Class "YourCustomClassCellName" selected in the Drop Down Menu. With your UITableViewCell selected in Interface Builder, make sure your Identifier is "YourCellName" and then in the func tableView(_ tableViewL UITableView, cellForRowAt) method before the return cell that you have cell.backgroundColor = UIColor.clear i.e,

确保在身份检查器中,您在下拉菜单中选择了自定义类 -> 类“YourCustomClassCellName”。在界面生成器中选择 UITableViewCell 后,确保您的标识符是“YourCellName”,然后在返回单元格之前的 func tableView(_ tableViewL UITableView, cellForRowAt) 方法中,您有 cell.backgroundColor = UIColor.clear 即,

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellName", for: indexPath) as? YourCellNameClass {
        cell.configureCell(parameter: ClassDataModel[indexPath.row])
        cell.backgroundColor = UIColor.clear
        return cell
    } else {
        return UITableViewCell()
    } 

I had the same issue and wrote all the code before I checked to see make sure my CellClass was selected and face palmed when I spent an hour wondering why it worked on another viewController and not in this instance. Even if you use IB to clear out the default white backgrounds, you still have to write the cell.backgroundColor = UIColor.clear. I was like yep, I found another bug in 8.2.1, but no just an iD10t operator bug.

当我花了一个小时想知道为什么它在另一个 viewController 上工作而不是在这个实例中时,我遇到了同样的问题并在检查以确保我的 CellClass 被选中并面对手掌之前编写了所有代码。即使你用IB清除了默认的白色背景,你还是要写cell.backgroundColor = UIColor.clear。我就像是的,我在 8.2.1 中发现了另一个错误,但不仅仅是 iD10t 操作员错误。

回答by Thinesh

I tested using swift 3. It's working

我使用 swift 3 进行了测试。它正在工作

uiTableName.backgroundView = nil
uiTableName.backgroundColor = UIColor.clear

回答by Ahsan Ebrahim

Setting these wont be enough as you have not applied clear colorto the contentViewof the tableViewand so it is coloring white in it. Try this in cellForRowAtIndexPath

设置这些还不够,因为您还没有应用clear color到 的contentViewtableView因此它在其中着色为白色。在 cellForRowAtIndexPath 中试试这个

cell.contentView.backgroundColor = UIColor .clearColor()

cell.contentView.backgroundColor = UIColor .clearColor()

回答by Bar?? ?z?elik

Maybe, you can check your table view's alpha.

也许,您可以检查表视图的 alpha。