[英]How do I make this UITableView clear (transparent) in Swift 3
我如何在 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?
注意:以下代码在Swift 3
进行了测试。
方法一:
从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
}
你可以试试这个
在viewDidLoad 中:
tableView.backgroundColor = UIColor.clear
在cellForRowAt 中:
cell.contentView.backgroundColor = UIColor.clear
这对我有用。
您必须将表格和单元格背景都设置为清晰。
在 tableView 函数中:
tableView.backgroundColor = .clear
cell.backgroundColor = .clear
tableView.tableFooterView = UIView()
前两个答案对我不起作用,所以我在各处添加了清晰的背景,白色背景消失了。
cell.layer.backgroundColor = UIColor.clear.cgColor
cell.backgroundColor = .clear
tableView.layer.backgroundColor = UIColor.clear.cgColor
tableView.backgroundColor = .clear
斯威夫特 5.1.2
为此,您必须实现一个新的 tableview 方法:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell,
forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor.clear
}
您缺少设置单元格 contentView 的背景颜色。
cell.contentView.backgroundColor = UIColor.clear
也许,您可以检查表视图的 alpha。
设置这些是不够的,因为您没有对tableView
的contentView
应用clear color
,因此它在其中着色为白色。 在 cellForRowAtIndexPath 中试试这个
cell.contentView.backgroundColor = UIColor .clearColor()
确保在身份检查器中,您在下拉菜单中选择了自定义类 -> 类“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()
}
当我花了一个小时想知道为什么它在另一个 viewController 上工作而不是在这个实例中时,我遇到了同样的问题并在检查以确保我的 CellClass 被选中并面对手掌之前编写了所有代码。 即使你用IB清除了默认的白色背景,你还是要写cell.backgroundColor = UIColor.clear。 我就像是的,我在 8.2.1 中发现了另一个错误,但不仅仅是 iD10t 操作员错误。
我使用 swift 3 进行了测试。它正在工作
uiTableName.backgroundView = nil
uiTableName.backgroundColor = UIColor.clear
在 Swift 4 中,在所需类的 viewDidLoad 下编写以下代码:-
tableView.backgroundColor = UIColor.clear
在你的tableView的cellForRowAt即dataSource下写下以下代码:-
let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifierName", for: indexPath) as! UITableViewCell //Cell ClassName
cell.layer.backgroundColor = UIColor.clear.cgColor
return cell
为什么没人说这个?:
@IBOutlet weak var tableView: UITableView!
// Drag and drop the tableView from the storyboard
override func viewDidLoad() {
super.viewDidLoad()
tableView.isHidden = true
}
结果让您有机会用一行隐藏整个 tableView。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.