在StackView中隐藏/显示Animate DatePicker

时间:2016-07-05 14:51:28

标签: ios swift uidatepicker uistackview

如何在stackView中正确设置datePicker外观/消失的动画效果?目前我试过这样:

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

这导致隐藏动画的问题 - 它很好地启动,然后最终datePickerView在datePicker所在的顶部闪烁一点。有什么建议吗?

2 个答案:

答案 0 :(得分:6)

我遇到了同样的问题并以这种方式解决了这个问题:

  • 将您的Picker放在视图中(我们称之为pickerContainerView)
  • 将216高度约束设置为pickerContainerView(选择器默认高度)
  • 将约束优先级设置为999以安静" UISV隐藏"约束警告
  • 添加"领先","尾随"和"垂直居中"从picker到pickerContainerView的约束
  • pickerContainerView的动画隐藏:

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

Views tree

答案 1 :(得分:0)

使用容器来容纳选择器并设置clipsToBounds = true对我有效。

我使用的是PureLayout,但它也适用于IB。

startRangePickerContainer = UIView()
startRangePickerContainer.clipsToBounds = true
startRangePickerContainer.backgroundColor = UIColor.cyan
stackView.addArrangedSubview(startRangePickerContainer)
startRangePickerContainer.autoPinEdge(toSuperviewEdge: .leading)
startRangePickerContainer.autoSetDimension(.height, toSize: 216)
startRangePickerContainer.autoPinEdge(toSuperviewEdge: .leading)
startRangePickerContainer.autoPinEdge(toSuperviewEdge: .trailing)

startRangePicker = UIDatePicker()
startRangePickerContainer.addSubview(startRangePicker)
startRangePicker.autoCenterInSuperview()

动画:

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