I want to animate the height constraint of a tableview I have. I have set up a small test project. The table view sits in a container view.
My code is like so:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var heightConstraint: NSLayoutConstraint!
@IBOutlet weak var contentView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
@IBAction func didTapAnimate(_ sender: Any) {
heightConstraint.constant = 400
UIView.animate(withDuration: 1.0, animations: {() in
self.contentView.layoutIfNeeded()
})
}
}
extension ViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.backgroundColor = UIColor.red
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
}
The problem is, when I hit the animate button the animation doesn't happen smoothly. The table first jumps up a certain height and then animates the rest. Example here:
What am I doing wrong here? Why does the table jump? I need the animation to be smooth. Any tips or pointers on this would be greatly appreciated! Thanks!