【问题标题】:Add an ImageView at the bottom of a UITableViewController在 UITableViewController 的底部添加一个 ImageView
【发布时间】:2018-06-15 09:15:18
【问题描述】:

我有一个 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

【问题讨论】:

  • 在表格视图下方添加 UIImageView 或 UIView。它不必在里面。只需将 tableview 的底部约束到另一个视图的顶部

标签: ios swift uitableview uiimage


【解决方案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】:
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerView = UIView()
        let imgView = UIImageView()
        //Write UIImageView required code
        footer.addSubView(imgView)
    
        return footerView
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    
           return HEIGHT_YOU_WANT
    }
    

    【讨论】:

      【解决方案3】:

      我会使用带有单独 UITableView 和 UIImageView 的 UIViewController 来执行此操作,而不是使用 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 只是为了不编写所有这些约束代码。重点在于逻辑。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-21
        • 2013-11-06
        • 1970-01-01
        • 2013-07-08
        • 1970-01-01
        相关资源
        最近更新 更多