问题
I have a problem that I can't create a view that can collapse it's contents that may be dynamic height , i know it's easy to collapse it with height constant = 0 , but i can't figure out how to make it expand again as the content of that view is dynamic and sub-itmes may be added later
I want that behavior
回答1:
Your answer has massively overcomplicated a very simple solution.
You should first create your zeroHeightConstraint
as a property.
let zeroHeightConstraint: NSLayoutConstraint = dynamicView.heightAnchor.constraint(equalToConstant: 0)
You could do this as an outlet from the storyboard too if you wanted.
Now add your button action...
@IBAction func toggleCollapisbleView() {
if animationRunning { return }
let shouldCollapse = dynamicView.frame.height != 0
animateView(isCollapse: shouldCollapse,
buttonText: shouldCollapse ? "Expand" : "Collapse")
}
private func animateView(isCollapse: Bool, buttonText: String) {
zeroHeightConstraint.isActive = isCollapse
animationRunning = true
UIView.animate(withDuration duration: 1, animations: {
self.view.layoutIfNeeded()
}, completion: { _ in
self.animationRunning = false
self.button.setTitle(buttonText, for: .normal)
})
}
Always avoid using view.tag
in your code. It should always be seen as "code smell". There is almost always a much more elegant way of doing what you are trying to do.
Regarding the bottom constraint. You should really be using a UIStackView
to layout the contents of the inside of the view here. By doing that you no longer need to iterate the subviews of your contentView
you can access the UIStackView
directly. And do the same thing there. In fact... a much better way would be to create the bottomConstraint
against the view using a priority
less than 1000
. e.g. 999
.
By doing that you don't have to remove or add that constraint at all. When the zeroHeightConstraint
is not active it will be there and work. When the zeroHeightConstraint
is active it will override the bottom constraint automatically.
来源:https://stackoverflow.com/questions/48150004/autolayouts-how-to-create-collapse-expand-dynamic-view-swift-3-4