简体   繁体   中英

Changing the height anchor of TableView based on the data/number of cells

I've been trying to change the TableView height to full screen if there is enough data otherwise make the height equal to the content (so you don't need to scroll). In the picture below, the tableview's height should end at the last cell but as you can see it's at the bottom of the view. I don't want the height of the circle view to change. (I know my tableview's bottom anchor is not set correctly but I'm running out of ideas)

Here is the current output

I've try changing the priority of the anchors, and various other things but could not get the desired result. I tried adding the code below after tableview.reloadData() but that caused the tableview's height anchor to be too large resulting in the height going off screen.

tableView.heightAnchor.constraint(equalToConstant: self.tableView.contentSize.height).isActive = true

Any help is greatly appreciated! Thanks

private func setupConstraint() {
        view.addSubview(tableView)
        view.addSubview(attendanceCircleView)

        NSLayoutConstraint.activate([
            attendanceCircleView.topAnchor.constraint(equalTo: view.topAnchor, constant: 10),
            attendanceCircleView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.95),
            attendanceCircleView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            attendanceCircleView.heightAnchor.constraint(equalToConstant: 240),
            tableView.topAnchor.constraint(equalTo: attendanceCircleView.bottomAnchor, constant: 10),
            tableView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.95),
            tableView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20)
        ])
    }

you can use below code.

@IBOutlet weak var tableViewHeightConstraint: NSLayoutConstraint! // create a height Constraint of tableview 


  override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.reloadTableView()
    }

  func updateTableiewConstraints() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.01, execute: {
            let height : CGFloat = self.tableView.contentSize.height
            self.tableViewHeightConstraint.constant = height
            UIView.animate(withDuration: 0.1, animations: {
                self.view.layoutIfNeeded()
            })
        })
    }

NOTE: 1. set tableview top constraint should be greater than 0.

  1. set priority of tableView height Constraint reduce to 900.

I solved my issue by doing the following.

let tableViewBottomAnchor = tableView.bottomAnchor.constraint(equalTo:view.bottomAnchor, constant: -20)
tableViewBottomAnchor.priority = .defaultLow
tableViewBottomAnchor.isActive = true

and calculating the height constraint after calling tableView.reloadData()

let tableViewMaxHeight = self.tableView.frame.height
let contentTableViewHeight = self.tableView.contentSize.height
let height = tableViewMaxHeight <= contentTableViewHeight ? tableViewMaxHeight: contentTableViewHeight
self.tableView.heightAnchor.constraint(equalToConstant:height).isActive = true
UIView.animate(withDuration: 0.1, animations: {
    self.view.layoutIfNeeded()
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM