如何在Swift 3中使这个UITableView清晰(透明) [英] How do I make this UITableView clear (transparent) in Swift 3
问题描述
如何制作这个 UITableView
并在Swift 3中清除它的单元格。
How do I make this UITableView
and it's cells clear in 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.
}
和我的手机台功能:
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显示了问题 - 请注意显示表格的黑线只是没有填充(因为没有人登录)
This gif shows the problem - notice the black lines showing the table is present just not populated (as no-one is logged in)
但是一秒钟就清楚了然后变成白色。
我哪里错了?
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文档说
...在iOS 7中,单元格默认为白色背景;在早期版本的iOS中,单元格继承封闭表视图的背景颜色。如果要更改单元格的背景颜色,请在tableView:willDisplayCell:forRowAtIndexPath:表视图委托方法中执行此操作。
... 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.
您可能需要使用willDisplayCell UITableView委托方法为表视图提供透明背景。
You might need to use willDisplayCell UITableView delegate method to have a transparent background for your table view .
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
如何应用上面的代码,因为它表示适用于iOS 7? / p>
How do I apply the code above as it says its for iOS 7?
推荐答案
注意:以下代码已在 Swift 3中测试
。
方法1:
从您的<选择您的tableViewCell code> storyboard 并转到属性Inspector
查看
更改背景
至清除
Select your tableViewCell from your storyboard
and goto Attributes Inspector
under View
change Background
to clear
方法2:在 cellForRowAt中尝试以下代码
cell.layer.backgroundColor = UIColor.clear.cgColor
注意:如果上述方法无效。按 shift + option + command + k $ c清除项目构建$ c>
Note : If above method didn't works.try clear your project build by pressing shift + option + command + k
更新:从以下代码更新您的 cellForRowAt
...
Update: Update your cellForRowAt
from below code...
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
}
这篇关于如何在Swift 3中使这个UITableView清晰(透明)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!