Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate DatePicker hide/show in StackView

How to properly animate datePicker appearance/disappearance in stackView? Currently I tried like this:

UIView.animateWithDuration(0.3, animations: {
    self.datePickerView.hidden = !self.datePickerView.hidden
})

This causes problems with hiding animation - it starts nicely and then in the end datePickerView flashes a little bit at the top of where datePicker was. Any suggestions?

like image 986
Xernox Avatar asked Jul 05 '16 14:07

Xernox


1 Answers

I had the same issue and solved it this way:

  • Put your Picker in a view (we will call it pickerContainerView)
  • Set a 216 height constraint to your pickerContainerView (picker default height)
  • Set the constraint priority to 999 to quiet "UISV-hiding" constraint warning
  • Add "leading", "trailing" and "center vertically" constraints from your picker to the pickerContainerView
  • animate hide of the pickerContainerView :

Swift 2

UIView.animateWithDuration(0.3, animations: {
    self.pickerContainerView.hidden = !self.pickerContainerView.hidden
})

Swift 3, 4, 5

UIView.animate(withDuration: 0.3, animations: {
    self.pickerContainerView.isHidden = !self.pickerContainerView.isHidden
})

Views tree

like image 74
Panda Avatar answered Sep 24 '22 00:09

Panda