gpt4 book ai didi

ios - UITableViewHeaderFooterView contentView 未设置为透明

转载 作者:行者123 更新时间:2023-11-28 08:20:53 25 4
gpt4 key购买 nike

我有一个带有背景图像的 ViewController,它作为 TableVC 作为它的 subview 。我希望 TableView 是透明的并且已经实现了,tableviewcell 也是透明的。但是我在让 tableviewheaderfooterview 透明时遇到了困难,因为当我将它的 contentview backgroundcolor 设置为 clearColor - 它显示灰色。

我正在使用 Xcode 7.3.1

private let collapsibleTableView : UITableView = {
let tv = UITableView()
tv.translatesAutoresizingMaskIntoConstraints = false
tv.backgroundColor = .clearColor()
tv.backgroundView = nil
tv.separatorStyle = .None
return tv
}()

此外,我已经将 UITableViewHeaderFooterView 子类化到我的自定义类,并且我还尝试将 contentView 背景颜色设置为 clearColor

override func layoutSubviews() {
super.layoutSubviews()
contentView.backgroundColor = .clearColor()
}

此外,我尝试在我的 VC 文件中再次设置它

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterViewWithIdentifier("header") as? CustomTableViewHeader ?? CustomTableViewHeader(reuseIdentifier: "header")
header.contentView.backgroundColor = .clearColor()
header.titleLabel.text = "custom-text"
header.delegate = self
return header
}

此外,我也将 tableviewcell 的背景设置为透明

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

cell.backgroundColor = .clearColor()
cell.contentView.backgroundColor = .clearColor()
}

但是不管我怎么做,headerview的背景都不会透明。

enter image description here

编辑:我也发现了这个 post关于 SO 上的相同问题,但实现建议的解决方案不起作用

 header.contentView.layer.backgroundColor = UIColor.clearColor().CGColor

最佳答案

试试这个,直接将背景颜色更改为您的 headerView,而不是更改您的 headerView.contentView 的颜色,对我来说,这个例子是可行的

 public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50))
view.backgroundColor = UIColor.clear
return view
}

因此,将 public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 方法中的代码更改为此

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterViewWithIdentifier("header") as? CustomTableViewHeader ?? CustomTableViewHeader(reuseIdentifier: "header")
header.backgroundColor = UIColor.clearColor()
header.titleLabel.text = "custom-text"
header.delegate = self
return header
}

希望对你有帮助

关于ios - UITableViewHeaderFooterView contentView 未设置为透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41334751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com