如何解决UITableViewCell不会更改其Alpha /不透明度
我有一个常规的表格视图,我希望它的单元格开始部分透明(alpha = 0.85)。但是,当我在cellForRowAt函数中设置Alpha时,单元格开始是完全不透明的。要指定,我希望整个单元格(包括其内容)为alpha = 0.85,而不仅仅是背景颜色。
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell",for: indexPath) as! customTableCell
cell.textLabel?.text = "\(indexPath.section)"
cell.backgroundColor = UIColor.white
cell.isOpaque = false
cell.alpha = CGFloat(0.85)
return cell
}
解决方法
尝试设置图层的不透明度:
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell",for: indexPath) as! customTableCell
cell.textLabel?.text = "\(indexPath.section)"
cell.backgroundColor = UIColor.white
cell.isOpaque = false
cell.layer.opacity = 0.85
return cell
}
,由于某种原因,tableView在cellForRowAt之后在一定程度上修改/重置了单元格的alpha和帧。还有一个名为tableView(_:willDisplay:forRowAt :)的UITableViewDelegate方法,该方法在显示单元格之前立即被触发。因此,我添加了以下代码,它起作用了:
func tableView(_ tableView: UITableView,willDisplay cell: UITableViewCell,forRowAt indexPath: IndexPath) {
cell.alpha = 0.85
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。