键盘覆盖视图底部的文本字段

我搜索过了

here: 仅当键盘覆盖输入字段时才向上移动视图

这里: 当键盘快速显示时移动文本字段

这里: 如何在键盘存在时使UITextField向上移动?

在这里: https : //developer.apple.com/library/content/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

不幸的是,所有链接和我能找到的其他地方似乎并没有提供我正在寻找的良好清洁解决方案。 它们要么用Obj-c代码过时,要么在使用Swift 4Xcode 9的当前迭代中不能工作。

如何管理覆盖视图底部文本字段的键盘? 我希望屏幕的视图只有在键盘覆盖文本字段视图时才会移动,而不使用滚动视图,能够对其进行动画处理以使其看起来漂亮而不是仅仅快照,最重要的是我不想使用外部图书馆

来自Apple的CoreAnimation库非常棒,但是所有的示例代码都已过时,并且在目标c中已经过时了(我不相信Apple没有更新他们的文档)。

如果有人能指出我正确的方向更新和当前的代码或Apple的图书馆我错过了将专门解决这个问题,我将不胜感激。

这段代码可以正常工作,如果它的框架与键盘相交并动画回到键盘隐藏的原始位置,则将textField设置为键盘上方的动画。

@IBOutlet weak var textField: UITextField! var offsetY:CGFloat = 0 override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardFrameChangeNotification(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil) } func keyboardFrameChangeNotification(notification: Notification) { if let userInfo = notification.userInfo { let endFrame = userInfo[UIKeyboardFrameEndUserInfoKey] as? CGRect let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double ?? 0 let animationCurveRawValue = (userInfo[UIKeyboardAnimationCurveUserInfoKey] as? Int) ?? Int(UIViewAnimationOptions.curveEaseInOut.rawValue) let animationCurve = UIViewAnimationOptions(rawValue: UInt(animationCurveRawValue)) if let _ = endFrame, endFrame!.intersects(self.textField.frame) { self.offsetY = self.textField.frame.maxY - endFrame!.minY UIView.animate(withDuration: animationDuration, delay: TimeInterval(0), options: animationCurve, animations: { self.textField.frame.origin.y = self.textField.frame.origin.y - self.offsetY }, completion: nil) } else { if self.offsetY != 0 { UIView.animate(withDuration: animationDuration, delay: TimeInterval(0), options: animationCurve, animations: { self.textField.frame.origin.y = self.textField.frame.origin.y + self.offsetY self.offsetY = 0 }, completion: nil) } } } } 

您可以使用IQKeyboardManagerSwift轻松快速地解决您的问题。

在pod文件中使用下面的pod,它支持Swift 4。

 pod 'IQKeyboardManagerSwift', '5.0.0' 

这是实现IQKeyboardManagerSwift的链接。

https://github.com/hackiftekhar/IQKeyboardManager

谢谢!!!

为我完美工作: http : //www.seemuapps.com/move-uitextfield-when-keyboard-is-presented

如果代表设置正确,

  func textFieldDidBeginEditing(_ textField: UITextField) { moveTextField(textField, moveDistance: -250, up: true) } // Finish Editing The Text Field func textFieldDidEndEditing(_ textField: UITextField) { moveTextField(textField, moveDistance: -250, up: false) } // Hide the keyboard when the return key pressed func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder() return true } // Move the text field in a pretty animation! func moveTextField(_ textField: UITextField, moveDistance: Int, up: Bool) { let moveDuration = 0.3 let movement: CGFloat = CGFloat(up ? moveDistance : -moveDistance) UIView.beginAnimations("animateTextField", context: nil) UIView.setAnimationBeginsFromCurrentState(true) UIView.setAnimationDuration(moveDuration) self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement) UIView.commitAnimations() } 

向Uiview添加一个扩展:

 import UIKit 

//将视图绑定到键盘更改扩展名UIView {

 func bindToKeyboard(){ NotificationCenter.default.addObserver(self, selector: #selector(UIView.keyboardWillChange(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil) } @objc func keyboardWillChange(_ notification: NSNotification) { let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt let curFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue let targetFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue let deltaY = targetFrame.origin.y - curFrame.origin.y UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: { self.frame.origin.y += deltaY },completion: {(true) in self.layoutIfNeeded() }) } 

}

这条代码对我有用。

如果是多个文本字段

我只针对位于底部的文本字段实现(不使用通知观察者)。

如果您使用的是scrollView ,则此代码可能会有所帮助( scrollViewDidScroll是可选的)

 func scrollViewDidScroll(_ scrollView: UIScrollView) { scrollView.contentSize = CGSize(width: self.scrollView.frame.size.width, height: (scrollView.frame.size.height + 300))// To be more specific, I have used multiple textfields so wanted to scroll to the end.So have given the constant 300. } func textFieldDidBeginEditing(_ textField:UITextField) { self.scrollView.setContentOffset(textField.frame.origin, animated: true) } 

如果要根据视图设置文本字段位置,请尝试以下操作:

 func textFieldDidBeginEditing(_ textField:UITextField){ textField.frame.origin.y = textField.frame.origin.y - 150 //(If have not used contentsizing the scroll view then exclude this line)default origin takes the texfield to the top of the view.So to set lower textfields to proper position have used the constant 150. self.scrollView.setContentOffset(textField.frame.origin, animated: true) } 

专门针对底部的文本字段。 检查textFieldDidBeginEditing中的标记值textfield.tag

 func textFieldDidBeginEditing(_ textField:UITextField){ if textField.tag = 4 { //tag value of the textfield which are at the bottom self.scrollView.setContentOffset(textField.frame.origin, animated: true) } } 

如果您在tableView中实现了文本字段,请使用通知观察器,如下所述。

如果tableView中有多个文本字段,最好使用Notification Observer

 override func viewDidAppear(_ animated: Bool) { NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil) } @objc func keyboardWillShow(notification: NSNotification) { if let keyboardHeight = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height { self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardHeight, 0) } } @objc func keyboardWillHide(notification: NSNotification) { UIView.animate(withDuration: 0.2, animations: { // For some reason adding inset in keyboardWillShow is animated by itself but removing is not, that's why we have to use animateWithDuration here self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0) }) } deinit { print("denit") NotificationCenter.default.removeObserver(self) }