设置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向上移动?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)