Animated view, changing constant constants

I have a datePicker that I want to animate inside and out from below, changing its "upper limit to super top".

I have IBOutlet installed, and on viewDidLoad I am allowed to change the restriction constant.

enter image description here

  override func viewDidLoad() {
    super.viewDidLoad()
    self.datePickerTopConstraint.constant = self.view.frame.size.height // I can set this to whatever and it will persist
  }

However, through IBAction, I am trying to set the constant to a different value, and this is not saved.

@IBAction func showDatePicker() {
    UIView.animateWithDuration(0.25, animations: {
      () -> Void in
      self.datePickerTopConstraint.constant = self.view.frame.size.height - self.datePicker.frame.size.height // Doesn't persist
      self.view.layoutIfNeeded()
    })
  }

It seems that I can undo this and show the datePicker in the view (in viewDidLoad) and animate it out of sight, but not have the datePicker that appears out of sight (as in the example above) and animate inside the view. What did I miss?

EDIT

- ( - ) 0, , , showDatePicker .

+5
2

, button , . . , self.datePicker.frame.size.height 0, .

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    datePickerTopConstraint.constant = constantForDatePickerViewHeightConstraint()
    view.setNeedsLayout()

}


@IBAction func showDatePicker(button: UIButton) {

    // Check constraint constant
    if datePickerTopConstraint.constant == self.view.frame.size.height {

        // Date picker is NOT visible
        datePickerTopConstraint.constant = constantForDatePickerViewHeightConstraint()

    } else {
        // Date picker is visible
        datePickerTopConstraint.constant = self.view.frame.size.height
    }


    UIView.animateWithDuration(0.25,
        animations: {() -> Void in
        self.view.layoutIfNeeded()
    })
}

private func constantForDateOPickerViewHeightConstraint() -> CGFloat {

    var value : CGFloat = 200.0

    // Workout value you want to as the constant for the constraint.

    return value
}
+4

:

func showDatePicker() {
    self.view.layoutIfNeeded()
    UIView.animateWithDuration(0.25, animations: {
      () -> Void in
      self.datePickerTopConstraint.constant = self.view.frame.size.height - self.datePicker.frame.size.height // Doesn't persist
      self.view.layoutIfNeeded()
    })
  }

layoutIfNeeded bock . . , viewDidLoad, viewWillAppear. viewDidLoad, , . layoutIfNeeded , , .

0

Source: https://habr.com/ru/post/1621966/


All Articles