fla*_*ara 4 keyboard view textfield ios swift
我搜索过了
here:仅当键盘覆盖输入字段时才向上移动视图
在这里:https://developer.apple.com/library/content/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html
不幸的是,所有链接和我能找到的其他地方似乎并没有提供我正在寻找的良好清洁解决方案.它们要么用Obj-c代码过时,要么在使用Swift 4的Xcode 9的当前迭代中不能工作.
如何管理覆盖视图底部文本字段的键盘?我希望屏幕的视图只有当键盘覆盖文本字段视图时才会移动,而不使用滚动视图,能够对其进行动画处理以使其看起来很漂亮,而不是让它快速捕捉,最重要的是我不想使用外部图书馆.
来自Apple的CoreAnimation库非常棒,但是所有的示例代码都已过时,并且在目标c中已经过时了(我不相信Apple没有更新他们的文档).
如果有人能够指出我正确的方向更新和当前的代码或Apple的图书馆我错过了将专门解决这个问题,我将不胜感激.
小智 7
您可以使用IQKeyboardManagerSwift轻松快速地解决您的问题.
在pod文件中使用下面的pod,它支持Swift 4.
pod 'IQKeyboardManagerSwift', '5.0.0'
Run Code Online (Sandbox Code Playgroud)
这是实现IQKeyboardManagerSwift的链接.
https://github.com/hackiftekhar/IQKeyboardManager
谢谢!!!
小智 6
此代码将起作用,使您的 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)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12930 次 |
| 最近记录: |