Swift ios 显示键盘时移动文本字段
Swift ios move text field when keyboard is shown
我是 ios/swift 的新手,不知道在文本字段/键盘方面该怎么做。
当我在键盘上单击文本字段时 blocks/covers 它,所以我不能 select 它或它下面的任何其他文本字段。
那么最好的解决方案是什么?除了将所有内容包装在滚动视图中。
我找到了这个片段,但我不确定如何实现它:
func textFieldDidBeginEditing(textField: UITextField) {
animateViewMoving(true, moveValue: 100)
}
func textFieldDidEndEditing(textField: UITextField) {
animateViewMoving(false, moveValue: 100)
}
func animateViewMoving (up:Bool, moveValue :CGFloat){
var movementDuration:NSTimeInterval = 0.3
var movement:CGFloat = ( up ? -moveValue : moveValue)
UIView.beginAnimations( "animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration )
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
UIView.commitAnimations()
}
或者如果有人从 github 那里得到了好的代码 example/library,请分享 :)
提前致谢,
首先,您应该注册两个键盘通知:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
然后,您应该将文本字段嵌入 UIView
(我们称之为 inputView
)。接下来,您应该引用视图的顶部(或底部约束)。这是具有底部约束的示例:
@IBOutlet weak var inputViewBottomConstraint: NSLayoutConstraint!
接下来实现两个观察者函数:
func keyboardWillShow(notification : NSNotification) {
var keyboardSize = notification.userInfo?[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size
previousConstant = self.inputViewBottomConstraint.constant
self.inputViewBottomConstraint.constant = keyboardSize!.height
UIView.animateWithDuration(0.3, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
self.layoutIfNeeded()
})
}
func keyboardWillHide(notification : NSNotification) {
self.inputViewBottomConstraint.constant = previousConstant
self.layoutIfNeeded()
}
Swift 3.0 - 如果你的 textField 在 UITableView
NotificationCenter.default.addObserver(self, selector:#selector(youViewContorllerName.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector:#selector(youViewContorllerName.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
func keyboardWillShow(notification : NSNotification) {
let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue.size
self.constraintTblBottom.constant = keyboardSize.height
UIView.animate(withDuration: 0.3, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: { () -> Void in
self.layoutIfNeeded()
})
}
func keyboardWillHide(notification : NSNotification) {
self.constraintTblBottom.constant = 0
self.layoutIfNeeded()
}
我是 ios/swift 的新手,不知道在文本字段/键盘方面该怎么做。
当我在键盘上单击文本字段时 blocks/covers 它,所以我不能 select 它或它下面的任何其他文本字段。
那么最好的解决方案是什么?除了将所有内容包装在滚动视图中。
我找到了这个片段,但我不确定如何实现它:
func textFieldDidBeginEditing(textField: UITextField) {
animateViewMoving(true, moveValue: 100)
}
func textFieldDidEndEditing(textField: UITextField) {
animateViewMoving(false, moveValue: 100)
}
func animateViewMoving (up:Bool, moveValue :CGFloat){
var movementDuration:NSTimeInterval = 0.3
var movement:CGFloat = ( up ? -moveValue : moveValue)
UIView.beginAnimations( "animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration )
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
UIView.commitAnimations()
}
或者如果有人从 github 那里得到了好的代码 example/library,请分享 :)
提前致谢,
首先,您应该注册两个键盘通知:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
然后,您应该将文本字段嵌入 UIView
(我们称之为 inputView
)。接下来,您应该引用视图的顶部(或底部约束)。这是具有底部约束的示例:
@IBOutlet weak var inputViewBottomConstraint: NSLayoutConstraint!
接下来实现两个观察者函数:
func keyboardWillShow(notification : NSNotification) {
var keyboardSize = notification.userInfo?[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size
previousConstant = self.inputViewBottomConstraint.constant
self.inputViewBottomConstraint.constant = keyboardSize!.height
UIView.animateWithDuration(0.3, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
self.layoutIfNeeded()
})
}
func keyboardWillHide(notification : NSNotification) {
self.inputViewBottomConstraint.constant = previousConstant
self.layoutIfNeeded()
}
Swift 3.0 - 如果你的 textField 在 UITableView
NotificationCenter.default.addObserver(self, selector:#selector(youViewContorllerName.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector:#selector(youViewContorllerName.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
func keyboardWillShow(notification : NSNotification) {
let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue.size
self.constraintTblBottom.constant = keyboardSize.height
UIView.animate(withDuration: 0.3, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: { () -> Void in
self.layoutIfNeeded()
})
}
func keyboardWillHide(notification : NSNotification) {
self.constraintTblBottom.constant = 0
self.layoutIfNeeded()
}