【问题标题】:Programmatic UIView inside TableView HeaderTableView Header 中的程序化 UIView
【发布时间】:2018-11-16 08:39:07
【问题描述】:

我以编程方式将 UIView 放入我的 TableView Header 中。

但是,标题视图被导航控制器栏挡住了,高度不会变大。

这是我的代码:

let headerView = UIView()

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    headerView.backgroundColor = UIColor(red: 0.000, green: 0.749, blue: 1.000, alpha: 1.00)
    headerView.frame = CGRect(x: 0, y: 100, width: view.frame.width, height: 200)
    return headerView
}

什么是有效的:

  1. TableView 加载。
  2. 出现一个“蓝色”标题,看起来大约 10 像素大并且卡在 TableView 的顶部
  3. 此“蓝色”标题已被导航控制器栏阻止。

什么不起作用:

  1. “蓝色”标题没有改变其高度。

  2. “蓝色”标题不会降低。

【问题讨论】:

  • 注意:这不是表头,而是节。改用 tableView.tableHeaderView 属性,然后设置框架将起作用
  • @KK7 你认为你可以用一些代码提供答案吗?抱歉,我只是新手。

标签: swift uitableview tableview


【解决方案1】:

应该是这样的!

var headerView = UIView()
headerView.backgroundColor = UIColor(red: 0.000, green: 0.749, blue: 1.000, alpha: 1.00)
headerView.frame = CGRect(x: 0, y: 100, width: view.frame.width, height: 200)
self.tableView.tableHeaderView = headerView // Here goes the UIView for the header

【讨论】:

    【解决方案2】:

    您可以通过两种方式实现此目的:

    1. 使用viewForHeaderInSection dataSource 和heightForHeaderInSection 委托方法。

    2. 但是,如果您要拥有一个标题并且不需要我建议使用的“粘性”部分标题功能:

      let tableHeaderView = UIView()
      override func viewDidLoad() {
          super.viewDidLoad()        
          tableHeaderView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 200)
          tableView.tableHeaderView = tableHeaderView
      }
      

    注意:如果您要采用 2 种方法,则 viewForHeaderInSection 在您的情况下是多余的,因此您应该将其删除。

    【讨论】:

      猜你喜欢
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 1970-01-01
      相关资源
      最近更新 更多