我有一个datePicker,我希望通过将其顶部约束更改为超级视图顶部来从底部进行动画制作. 我有一个IBOutlet设置它和viewDidLoad我被允许更改约束常量. override func viewDidLoad() { super.viewDidLoad()
我有一个IBOutlet设置它和viewDidLoad我被允许更改约束常量.

override func viewDidLoad() {
super.viewDidLoad()
self.datePickerTopConstraint.constant = self.view.frame.size.height // I can set this to whatever and it will persist
}
但是,通过IBAction,我尝试将常量设置为另一个值并且不会持续存在.
@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()
})
}
似乎我可以反转这个并让datePicker出现在视图中(在viewDidLoad中)并将其设置为视图外的动画,但不要让datePicker出现在视图之外(如上例所示)并在视图内部设置动画.我错过了什么?
编辑
通过将顶部约束常量设置为超视图的高度I(由于某种原因我不明白),还将日期选择器的高度设置为0,这反过来使得showDatePicker中的减法无意义.
重构代码,以便在按钮的方法工作中首先计算常量的新值,然后调用动画.将高度计算拉入其自身的功能.我认为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
}
