Autolayouts : How to create collapse/Expand dynamic view swift 3/4

后端 未结 1 1875
Happy的楠姐
Happy的楠姐 2021-01-31 13:13

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

1条回答
  •  旧巷少年郎
    2021-01-31 13:21

    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.

    0 讨论(0)
提交回复
热议问题