3 回答

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊
這是一個(gè)解決方案,無需處理從一個(gè)textField到另一個(gè)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?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() { self.view.frame.origin.y -= keyboardSize.height } }func keyboardWillHide(notification: NSNotification) { self.view.frame.origin.y = 0}
要解決此問題,請keyboardWillShow/Hide
使用以下代碼替換這兩個(gè)函數(shù):
func keyboardWillShow(notification: NSNotification) { if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] 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?[UIKeyboardFrameBeginUserInfoKey] 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?[UIKeyboardFrameBeginUserInfoKey] 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.keyboardFrameBeginUserInfoKey] 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 }}

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊
最簡單的方法,甚至不需要任何代碼:
如果您還沒有使用Spring動(dòng)畫框架,請下載KeyboardLayoutConstraint.swift并將文件添加(拖放)到您的項(xiàng)目中。
在故事板中,為視圖或文本字段創(chuàng)建底部約束,選擇約束(雙擊它),然后在Identity Inspector中,將其類從NSLayoutConstraint更改為KeyboardLayoutConstraint。
完成!
該對象將與鍵盤同步自動(dòng)向上移動(dòng)。

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊
此線程上的一個(gè)流行答案使用以下代碼:
func keyboardWillShow(sender: NSNotification) { self.view.frame.origin.y -= 150}func keyboardWillHide(sender: NSNotification) { self.view.frame.origin.y += 150}
將靜態(tài)量偏移視圖存在明顯問題。它在一臺(tái)設(shè)備上看起來不錯(cuò),但在任何其他尺寸配置上看起來都很糟糕。您需要獲得鍵盤高度并將其用作偏移值。
這是一個(gè)適用于所有設(shè)備的解決方案,可以處理用戶在鍵入時(shí)隱藏預(yù)測文本字段的邊緣情況。
解
需要注意的是,我們將self.view.window作為對象參數(shù)傳遞。這將為我們提供鍵盤數(shù)據(jù),例如它的高度!
@IBOutlet weak var messageField: UITextField!override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: self.view.window) NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: self.view.window)}func keyboardWillHide(sender: NSNotification) { let userInfo: [NSObject : AnyObject] = sender.userInfo! let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size self.view.frame.origin.y += keyboardSize.height}
我們將使它在所有設(shè)備上看起來都很漂亮,并處理用戶添加或刪除預(yù)測文本字段的情況。
func keyboardWillShow(sender: NSNotification) { let userInfo: [NSObject : AnyObject] = sender.userInfo! let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size let offset: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue.size if keyboardSize.height == offset.height { UIView.animateWithDuration(0.1, animations: { () -> Void in self.view.frame.origin.y -= keyboardSize.height }) } else { UIView.animateWithDuration(0.1, animations: { () -> Void in self.view.frame.origin.y += keyboardSize.height - offset.height }) }}
刪除觀察員
在離開視圖之前不要忘記刪除觀察者,以防止傳輸不必要的消息。
override func viewWillDisappear(animated: Bool) { NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: self.view.window) NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: self.view.window)}
根據(jù)評論中的問題進(jìn)行更新:
如果您有兩個(gè)或更多文本字段,則可以檢查view.frame.origin.y是否為零。
func keyboardWillShow(sender: NSNotification) { let userInfo: [NSObject : AnyObject] = sender.userInfo! let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size let offset: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue.size if keyboardSize.height == offset.height { if self.view.frame.origin.y == 0 { UIView.animateWithDuration(0.1, animations: { () -> Void in self.view.frame.origin.y -= keyboardSize.height }) } } else { UIView.animateWithDuration(0.1, animations: { () -> Void in self.view.frame.origin.y += keyboardSize.height - offset.height }) } print(self.view.frame.origin.y)}
- 3 回答
- 0 關(guān)注
- 854 瀏覽
添加回答
舉報(bào)