我有一个UITextview,它低于2个UITextFields和一个UISwitch。
如果启用了UISwitch,我想删除2个UITextField并将UITextView向上移动。如果再次关闭UISwitch,那么我希望返回2个UITextField,并返回UITextView的原始位置。
我使用以下代码实现了这一目标。
func allDaySwitched() {
if appointmentAllDayToggle.isOn {
appointmentStartTimeTextfield.tag = 0
appointmentStopTimeTextfield.tag = 0
appointmentSummaryTextfield.tag = 4
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.appointmentStartTimeLabel.alpha = 0
self.appointmentStopTimeLabel.alpha = 0
self.appointmentStartTimeTextfield.alpha = 0
self.appointmentStopTimeTextfield.alpha = 0
}, completion: { (true) in
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.appointmentSummaryTextfield.frame = CGRect(x: self.appointmentSummaryTextfield.frame.origin.x, y: self.appointmentSummaryTextfield.frame.origin.y - 50, width: self.appointmentSummaryTextfield.frame.width, height: self.appointmentSummaryTextfield.frame.height)
self.appointmentSummaryLabel.frame = CGRect(x: self.appointmentSummaryLabel.frame.origin.x, y: self.appointmentSummaryLabel.frame.origin.y - 50, width: self.appointmentSummaryLabel.frame.width, height: self.appointmentSummaryLabel.frame.height)
}, completion: { (true) in
})
})
} else {
appointmentStartTimeTextfield.tag = 4
appointmentStopTimeTextfield.tag = 5
appointmentSummaryTextfield.tag = 6
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.appointmentSummaryTextfield.frame = CGRect(x: self.appointmentSummaryTextfield.frame.origin.x, y: self.appointmentSummaryTextfield.frame.origin.y + 50, width: self.appointmentSummaryTextfield.frame.width, height: self.appointmentSummaryTextfield.frame.height)
self.appointmentSummaryLabel.frame = CGRect(x: self.appointmentSummaryLabel.frame.origin.x, y: self.appointmentSummaryLabel.frame.origin.y + 50, width: self.appointmentSummaryLabel.frame.width, height: self.appointmentSummaryLabel.frame.height)
}, completion: { (true) in
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.appointmentStartTimeLabel.alpha = 1
self.appointmentStopTimeLabel.alpha = 1
self.appointmentStartTimeTextfield.alpha = 1
self.appointmentStopTimeTextfield.alpha = 1
}, completion: { (true) in
})
})
}
}
这非常有效,但是,只要UITextView聚焦,它就会返回原始位置。从那里,如果我再次切换UISwitch,那么它会进一步向下移动。
如何提交此更改,以便在聚焦时UITextView不会移动?我试过了:
updateConstraints()
layoutIfNeeded()
setNeedsLayout()
在动画完成事件中,但它们都没有提交更改?
我怎样才能做到这一点? - 我附上了行为截图
初始布局
切换时的布局(所需)
摘要字段聚焦时的布局(我已将照片中的字段重新排除以删除键盘) - 不需要
摘要字段聚焦后的布局,然后UISwitch被覆盖 - 不需要