使用Swift使用键盘移动视图

使用Swift使用键盘移动视图,第1张

使用Swift使用键盘移动视图

这是一个解决方案,无需处理从一个textField到另一个的切换:

 override func viewDidLoad() {        super.viewDidLoad()        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)     }func keyboardWillShow(notification: NSNotification) {     if let keyboardSize = (notification.userInfo?[UIKeyboardframeEndUserInfoKey] as? NSValue)?.CGRectValue() {        self.view.frame.origin.y -= keyboardSize.height    } }func keyboardWillHide(notification: NSNotification) {    self.view.frame.origin.y = 0}

要解决此问题,请用以下两个函数

keyboardWillShow/Hide
替换它们:

func keyboardWillShow(notification: NSNotification) { if let keyboardSize = (notification.userInfo?[UIKeyboardframeEndUserInfoKey] as? NSValue)?.CGRectValue() {        if view.frame.origin.y == 0 { self.view.frame.origin.y -= keyboardSize.height        }    }        }func keyboardWillHide(notification: NSNotification) {    if view.frame.origin.y != 0 {        self.view.frame.origin.y = 0    }}
编辑SWIFT 3.0:
override func viewDidLoad() {    super.viewDidLoad()     NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)    }@objc func keyboardWillShow(notification: NSNotification) { if let keyboardSize = (notification.userInfo?[UIKeyboardframeEndUserInfoKey] as? NSValue)?.cgRectValue {        if self.view.frame.origin.y == 0 { self.view.frame.origin.y -= keyboardSize.height        }    }        }@objc func keyboardWillHide(notification: NSNotification) {    if self.view.frame.origin.y != 0 {        self.view.frame.origin.y = 0    }}

编辑SWIFT 4.0:

override func viewDidLoad() {    super.viewDidLoad()     NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)    }@objc func keyboardWillShow(notification: NSNotification) { if let keyboardSize = (notification.userInfo?[UIKeyboardframeEndUserInfoKey] as? NSValue)?.cgRectValue {        if self.view.frame.origin.y == 0 { self.view.frame.origin.y -= keyboardSize.height        }    }        }@objc func keyboardWillHide(notification: NSNotification) {    if self.view.frame.origin.y != 0 {        self.view.frame.origin.y = 0    }}
编辑SWIFT 4.2:
override func viewDidLoad() {    super.viewDidLoad()     NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)}@objc func keyboardWillShow(notification: NSNotification) {    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardframeEndUserInfoKey] as? NSValue)?.cgRectValue {        if self.view.frame.origin.y == 0 { self.view.frame.origin.y -= keyboardSize.height        }    }}@objc func keyboardWillHide(notification: NSNotification) {    if self.view.frame.origin.y != 0 {        self.view.frame.origin.y = 0    }}


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

原文地址: http://outofmemory.cn/zaji/5674026.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存