How to change Auto Layout constraints after they are set when using constraintEqualToAnchor()?

前端 未结 4 1351
有刺的猬
有刺的猬 2021-02-04 02:15

I try to set up a view with AutoLayout constraints by using constraintEqualToAnchor():

override func viewDidLoad() {
    super.viewDidLoad()

    le         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-04 03:09

    Here is example with if statement modifying a StackView and View when a segmented clicked:

    if (sender as AnyObject).selectedSegmentIndex == 0{
        // Shrink the white view and stack view
        heightConstraintView = containerView.heightAnchor.constraint(equalToConstant: 100)
        heightConstraintView?.isActive = true
        heightConstraintStackView = stackView.heightAnchor.constraint(equalToConstant: 100)
        heightConstraintStackView?.isActive = true
    } else {
        // Before returning back the white view and stack view DEACTIVATE teh previous constraints
        heightConstraintView?.isActive = false
        heightConstraintStackView?.isActive = false
        // Returning back the white view and stack view to normal size
        heightConstraintView = containerView.heightAnchor.constraint(equalToConstant: 200)
        heightConstraintView?.isActive = true
        heightConstraintStackView = stackView.heightAnchor.constraint(equalToConstant: 200)
        heightConstraintStackView?.isActive = true
    }
    

提交回复
热议问题