我有一个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}总结
以上是内存溢出为你收集整理的swift – 通过更改约束常量来设置Animate视图全部内容,希望文章能够帮你解决swift – 通过更改约束常量来设置Animate视图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)