IQKeyboardManager
キーボードで入力した後、テキストフィールドが上がるように使用しています。
テキスト フィールドをクリックしても、特定のビューにスクロールしたくありません。以下はデザインのスクリーンショットです。「ヘッダー」を一番上に残したい。
彼らのドキュメンテーションから、ナビゲーションバーを一番上に保つ方法があります。
IQKeyboardManager
キーボードで入力した後、テキストフィールドが上がるように使用しています。
テキスト フィールドをクリックしても、特定のビューにスクロールしたくありません。以下はデザインのスクリーンショットです。「ヘッダー」を一番上に残したい。
彼らのドキュメンテーションから、ナビゲーションバーを一番上に保つ方法があります。
ViewController の IQKeyboardManager を無効にします。
そのために、
IQKeyboardManager.sharedManager().disableInViewControllerClass(ViewController.self)
そして、そのviewControllerに次のコードを書きます。キーボードの高さに応じてビューを上に移動します
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)
}
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
}
}
}
func keyboardWillHide(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
}
}
}
「 HEADER」ビューをTOPに残しておきたい場合は、
このようにしてください:
**
YourViewController.view -> [headerView][contentView]
**
textfieldを [ contentView ] に配置し、上記のコードの Self.view の代わりに [contentView].y を変更します。
を無効にIQKeyboardManager
しますviewController
。
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
IQKeyboardManager.sharedManager().enable = false
NotificationCenter.default.addObserver(self, selector: #selector(Login.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(Login.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
ハンドル キーボード:
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0{
self.table_view.frame.origin.y -= keyboardSize.height
}
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.table_view.frame.origin.y += keyboardSize.height
}
}
}
オブザーバーを削除します。
override func viewWillDisappear(animated: Bool) {
IQKeyboardManager.sharedManager().enable = true
NSNotificationCenter.defaultCenter().removeObserver(self)
}