在UITableViewController的底部添加一个ImageView

时间:2018-06-15 09:15:19

标签: ios swift uitableview uiimage

我有一个TableViewController类: -

class SideMenuViewController: UITableViewController {

     let tableViewFooter = UIView(frame: CGRect(x: 0, y: self.view.bounds.size.height + 80, width: self.view.bounds.width, height: 80))

     tableViewFooter.backgroundColor = UIColor(patternImage: UIImage(named: "nav_drawer_footer.jpg")!)
     tableView.tableFooterView  = tableViewFooter

}

但使用此代码..视图位于tableView的最后一个单元格下....我不想要这个。

我需要的是桌面视图控制器底部的ImageView / UIView

3 个答案:

答案 0 :(得分:2)

public onCheckboxStateChange(changeEvent: MatCheckboxChange, id: number) {
    const index = this.companies.findIndex(x => x.id === id);

    if (index === -1) {
        console.warn("Company not found");
        return;
    }

    companies[index].checked = changeEvent.checked;
}

答案 1 :(得分:1)

    let theHeight = view.frame.size.height //grabs the height of your view
    let footer = UIView()
    footer.backgroundColor = UIColor(patternImage: UIImage(named: "nav_drawer_footer.jpg")!)
    footer.frame = CGRect(x: 0, y: theHeight - 150 , width: self.view.frame.width, height: 80)
    self.view.addSubview(footer)

答案 2 :(得分:-1)

我会用UIViewController和单独的UITableView和UIImageView而不是UITableViewController这样做。当使用UITableViewController时,self.view实际上是self.tableView,即你的图像将是UITableView的子视图,它将与底行重叠。使用布局约束比使用框架约束更好。由于约束代码有点难以写,您可以像我在这里一样使用SnapKit

import SnapKit

class SideMenuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    let tableView = UITableView()
    let tableViewFooter = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.delegate = self
        tableView.dataSource = self

        tableViewFooter.backgroundColor = UIColor(patternImage: UIImage(named: "nav_drawer_footer.jpg")!)

        view.addSubview(tableView)
        view.addSubview(tableViewFooter)
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        tableViewFooter.snp.makeConstraints { (make) -> Void in
            make.width.equalTo(view.snp.width)
            make.left.equalTo(view.snp.left)
            make.bottom.equalTo(view.snp.bottom)
            make.height.equalTo(80)
        }

        tableView.snp.makeConstraints { (make) in
            make.top.equalTo(view.snp.top)
            make.bottom.equalTo(tableViewFooter.snp.top)
            make.width.equalTo(view.snp.width)
            make.centerX.equalTo(view.snp.centerX)
        }

    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.backgroundColor = UIColor.blue
        return cell
    }

}

编辑:我正在使用SnapKit,因此我没有编写所有这些约束代码。重点在于逻辑。

相关问题