Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法将tableHeaderView高度设置为与表格视图边框高度相同_Ios_Swift_Uitableview - Fatal编程技术网

Ios 无法将tableHeaderView高度设置为与表格视图边框高度相同

Ios 无法将tableHeaderView高度设置为与表格视图边框高度相同,ios,swift,uitableview,Ios,Swift,Uitableview,我正在尝试创建一个与表视图高度相同的tableHeaderView。考虑下面的代码: private func setupViews() { view.addSubview(tableView) tableView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ tableView.topAnchor.constraint(equalTo: v

我正在尝试创建一个与表视图高度相同的
tableHeaderView
。考虑下面的代码:

private func setupViews() {

    view.addSubview(tableView)
    tableView.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        tableView.topAnchor.constraint(equalTo: view.topAnchor),
        tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
    ])

    // Call async else frame size will be incorrect
    DispatchQueue.main.async { [weak self] in
        guard let self = self else { return }
        self.tableView.tableHeaderView = self.makeTableHeaderView()
    }
}


private func makeTableHeaderView() -> UIView {
    let headerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: tableView.bounds.width, height: tableView.bounds.height))
    tableView.backgroundColor = .blue
    headerView.backgroundColor = .red
    return headerView
}
这会在表格底部产生额外的空间,并允许表格稍微滚动(不是表格滚动的图像):

为什么这里有额外的高度?我希望它与表视图框架的大小完全相同


任何提示或指点都将不胜感激

在异步块中调用
self.makeTableHeaderView
不是获取表视图正确帧的正确方法,因为只有在viewDidLayoutSubviews方法之后才能获取表视图的结果大小(此方法可能会被多次调用)。

更新标题视图大小的一个好地方是,但是您应该使用它来计算表视图的结果大小。

首先,您要尊重安全区域,否则您将在设备之间得到不一致的结果

其次,您需要在表格布局后设置标题视图的高度。如果/当表格框架发生变化(例如在设备上旋转),您可能还需要对其进行更改

因此,在控制器中实现
viewdilayoutsubviews()
,如下所示:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    
    if let headerView = tableView.tableHeaderView {
        
        // height of the table view
        let tableFrame = tableView.frame
        // current frame of the header view
        var headerFrame = headerView.frame
        
        //Comparison necessary to avoid infinite loop
        if tableFrame.size.height != headerFrame.size.height {
            // table view frame height has changed
            headerFrame.size.height = tableFrame.size.height
            headerView.frame = headerFrame
            tableView.tableHeaderView = headerView
        }
    }
}
下面是一个完整的示例:

class BigHeaderViewController: UIViewController {
    
    let tableView = UITableView()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupViews()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        if let headerView = tableView.tableHeaderView {
            
            // height of the table view
            let tableFrame = tableView.frame
            // current frame of the header view
            var headerFrame = headerView.frame
            
            //Comparison necessary to avoid infinite loop
            if tableFrame.size.height != headerFrame.size.height {
                // table view frame height has changed
                headerFrame.size.height = tableFrame.size.height
                headerView.frame = headerFrame
                tableView.tableHeaderView = headerView
            }
        }
    }
    
    private func setupViews() {
        
        view.addSubview(tableView)
        tableView.translatesAutoresizingMaskIntoConstraints = false
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            tableView.topAnchor.constraint(equalTo: g.topAnchor),
            tableView.leadingAnchor.constraint(equalTo: g.leadingAnchor),
            tableView.trailingAnchor.constraint(equalTo: g.trailingAnchor),
            tableView.bottomAnchor.constraint(equalTo: g.bottomAnchor)
        ])

        tableView.tableHeaderView = makeTableHeaderView()

        tableView.backgroundColor = .blue
    }
    
    private func makeTableHeaderView() -> UIView {
        let headerView = UIView()
        headerView.backgroundColor = .red
        return headerView
    }
}

我试着像这样在
viewDidLayoutSubviews
中设置高度,但仍然得到了错误的结果height@Kex-您需要更清楚地了解“错误的高度”。我发布的代码示例符合您在问题中提出的要求。你试过自己运行这个代码吗?