ios – 当键盘存在时如何使UITextField向上移动?

ios – 当键盘存在时如何使UITextField向上移动?,第1张

概述如何防止键盘隐藏UITextField? 我假设这是在UIViewController上发生的.如果是这样,您可以设置在键盘显示/隐藏时调用以下2个函数,并在其块中进行适当响应. 设置UIViewController: class XXXViewController: UIViewController, UITextFieldDelegate... { var frameView: UI 如何防止键盘隐藏UITextFIEld?解决方法 我假设这是在UIVIEwController上发生的.如果是这样,您可以设置在键盘显示/隐藏时调用以下2个函数,并在其块中进行适当响应.

设置UIVIEwController:

class XXXVIEwController: UIVIEwController,UITextFIEldDelegate... {    var frameVIEw: UIVIEw!

首先,在VIEwDIDLoad中:

overrIDe func vIEwDIDLoad() {    self.frameVIEw = UIVIEw(frame: CGRectMake(0,self.vIEw.bounds.wIDth,self.vIEw.bounds.height))    // Keyboard stuff.    let center: NSNotificationCenter = NSNotificationCenter.defaultCenter()        center.addobserver(self,selector: #selector(ATReportContentVIEwController.keyboarDWillShow(_:)),name: UIKeyboarDWillShowNotification,object: nil)    center.addobserver(self,selector: #selector(ATReportContentVIEwController.keyboarDWillHIDe(_:)),name: UIKeyboarDWillHIDeNotification,object: nil)}

然后实现以下2个函数以响应上面VIEwDIDLoad中定义的NSNotificationCenter函数.我给你一个移动整个视图的例子,但你也可以只为UITextFIElds制作动画.

func keyboarDWillShow(notification: NSNotification) {    let info:NSDictionary = notification.userInfo!    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()    let keyboardHeight: CGfloat = keyboardSize.height    let _: CGfloat = info[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber as CGfloat    UIVIEw.animateWithDuration(0.25,delay: 0.25,options: UIVIEwAnimationoptions.CurveEaseInOut,animations: {        self.frameVIEw.frame = CGRectMake(0,(self.frameVIEw.frame.origin.y - keyboardHeight),self.vIEw.bounds.height)        },completion: nil)}func keyboarDWillHIDe(notification: NSNotification) {    let info: NSDictionary = notification.userInfo!    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()    let keyboardHeight: CGfloat = keyboardSize.height    let _: CGfloat = info[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber as CGfloat    UIVIEw.animateWithDuration(0.25,(self.frameVIEw.frame.origin.y + keyboardHeight),completion: nil)}

不要忘记在离开视图时删除通知

overrIDe func vIEwWilldisappear(animated: Bool) {    NSNotificationCenter.defaultCenter().removeObserver(self,object: nil)    NSNotificationCenter.defaultCenter().removeObserver(self,object: nil)}
总结

以上是内存溢出为你收集整理的ios – 当键盘存在时如何使UITextField向上移动?全部内容,希望文章能够帮你解决ios – 当键盘存在时如何使UITextField向上移动?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1108607.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存