2

我正在尝试为 TableViewHeader 设置透明背景但没有成功。首先,想知道这是否可能?

这就是我所做的

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
   let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView 
   header.contentView.backgroundColor = UIColor.clearColor()
}

我的最终目标是在我的 uitableview 下添加一个 UIView,它将托管一个 GMSMapView 并具有一个大小为 100px 的透明 HeaderView。

有什么帮助吗?谢谢

4

3 回答 3

4

您需要执行以下步骤

1) 在情节提要中将 headerfooterview 添加到 tableview

2)鉴于确实加载写了下面的代码

tableView.registerClass(HeaderView.self, forHeaderFooterViewReuseIdentifier: "HeaderView")
headerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier("headerView") as? HeaderView

3)添加tableview委托方法

 func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 110;
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    headerView.backgroundColor = UIColor.clearColor()
    return headerView;
}

希望对你有帮助

于 2016-08-04T17:24:07.617 回答
2

要遵循的步骤:

1) 从对象库中拖放一个 UIView 到 tableview 的顶部以制作 tableview 标题。

2)在 Tableview 控制器中创建视图的 IBOutlet。

3)最后调用这个 UITableview 委托方法

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        yourHeaderView.backgroundColor = UIColor.clearColor()
        return yourHeaderView

    }

// 当你需要创建一个透明的页脚视图时调用它

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        footerView.backgroundColor = UIColor.clearColor()
        return footerView
    }
于 2016-08-04T17:44:24.473 回答
0

//为了透明你的表格视图部分标题:

//在 Swift 5 中

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

view.tintColor = UIColor.clear

}

于 2021-02-05T14:07:59.147 回答