Reputation: 77
I have a UIView animating dropdown while tapping. what I want to achieve is, while UIView height changing when I tap the tableView cell automatically follow the height too. this is my code
class SubjectCell: BaseCell {
lazy var tableView: UITableView = {
let tv = UITableView()
tv.delegate = self
tv.dataSource = self
tv.allowsSelection = false
return tv
}()
extension SubjectCell: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
}
// This is the view I want to animate
@IBOutlet weak var mainContainer: UIView!
@IBOutlet weak var calendarView: JKCalendar!
@IBOutlet weak var arrowImageView: UIImageView!
@IBOutlet weak var mainContainerHeightConstraint: NSLayoutConstraint!
override func awakeFromNib() {
super.awakeFromNib()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
mainContainer.addGestureRecognizer(tapGesture)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@objc func handleTap(gesture: UITapGestureRecognizer) {
if mainContainerHeightConstraint.constant == 75 {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.arrowImageView.image = #imageLiteral(resourceName: "up-chevron")
self.mainContainerHeightConstraint.constant = 370
self.calendarView.alpha = 1
})
} else {
defaultConstraint()
}
}
fileprivate func defaultConstraint() {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.arrowImageView.image = #imageLiteral(resourceName: "down-chevron")
self.mainContainerHeightConstraint.constant = 75
self.calendarView.alpha = 0
})
}
this is my configuration for the uiview.
I set my mainContainerView top, leading, and trailing = 0. and height = 370.
inside mainContainerView there is a stackView containing another 2 view, and set my stack view top, trailing, bottom, and leading = 0 in mainContainerView
this the result in my simulator
I still can't get the idea how to setup tableViewCell so it will automatically changing. Please help me :)
Upvotes: 0
Views: 106
Reputation: 324
You need to set the bottom constraint of mainContainerView
so that the row height will be calculated based on you constraints and remove the heightForRowAt
function. And another is every time you change the height of your mainContainerView
you need to call
tableView.beginUpdates()
tableView.endUpdates()
For it to recalculate the new height of your row and it will apply.
Upvotes: 1