在使用 swift 开发 ios 应用程序时,我遇到了这个问题。 当软键盘出现时,我需要调整 View 以显示隐藏的文本字段。文本字段在 iPhone 4s、5、5s 中被隐藏。但在 iPhone 6 及更高版本中,即使在键盘出现后它仍然完美。所以我想知道这种情况的解决方案。仅当文本字段/任何字段由于软键盘而隐藏时才调整 View ,否则就保持原样。
最佳答案
当您开始输入时,以下代码会将您的 View 向上移动 200 像素。当文本字段结束编辑时, View 将动画回到原始位置。 不要忘记为文本字段设置委托(delegate)。
override func textFieldDidBeginEditing(textField: UITextField) {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(0.3)
UIView.animationBeginsFromCurrentState = TRUE
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - 200.0, self.view.frame.size.width, self.view.frame.size.height)
UIView.commitAnimations()
}
func textFieldDidEndEditing(textField: UITextField)
{
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(0.3)
UIView.animationBeginsFromCurrentState = TRUE
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 200.0, self.view.frame.size.width, self.view.frame.size.height)
UIView.commitAnimations()
}
关于ios - 在iOS调整 View 中,当且仅当文本字段隐藏在键盘后面时才应该移动,否则不需要调整。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36862748/