在 IQKeyboardManager 中始终保持视图在顶部(不要使用键盘滚动)
Keep a view always on top (Don't scroll with keyboard) in IQKeyboardManager
我正在使用 IQKeyboardManager
让文本字段在使用键盘输入后保持上升。
即使单击文本字段,我也不想滚动到特定视图。下面是设计的截图。我希望 'header' 保持在顶部。
根据他们的文档,there is a way to keep the navigation bar remain on top.
为您的 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”视图保持在顶部,
这样做:
**
YourViewController.view -> [headerView][contentView]
**
将 textfield 放入 [contentView] 并更改 [contentView].y 而不是上面代码中的 Self.view。
为您的 viewController
禁用 IQKeyboardManager
:
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)
}
@Wolverine 和@Bhavin Ramani 的回答很棒:让您的 自定义 header 保持在顶部的最佳方法是手动操作键盘(根据IQKeyboardSwift 作者comment)。如果您使用 iOS 默认导航栏,它似乎是由图书馆为您处理的。
在这里我只想分享一些关于这个主题的更新,以供我将来参考,因为答案有点旧而且一些 Swift 语法已经改变。下面的代码是用 Xcode 13.2 编写的,针对 iOS 13+.
首先,您想通过
禁用 KQKeyboardManager
IQKeyboardManager.shared.enable = false
请注意,此行仅禁用 向上移动文本字段功能 ,其他 IQKeyboard 功能,如在外部触摸时退出、自动工具栏等,不会因此而禁用线,这通常是你想要的。
然后,您在视图控制器的 viewDidLoad
中注册键盘事件观察器,在 deinit
中删除观察器。
override func viewDidLoad() {
super.viewDidLoad()
IQKeyboardManager.shared.enable = false
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillShow),
name: UIResponder.keyboardWillShowNotification,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillHide),
name: UIResponder.keyboardWillHideNotification,
object: nil)
}
deinit {
IQKeyboardManager.shared.enable = true
NotificationCenter.default.removeObserver(self)
}
接下来,为键盘 show/hide 添加视图移动 up/down 方法。
@objc private func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect {
print("keyboardSize.height", keyboardSize.height)
// using the right key here is important, because
// keyboardFrameEndUserInfoKey is an user info key
// to retrieve the keyboard’s frame at the END of its animation.
// here you move up the views you need to move up
// if you use auto layout, update the corresponding constraints
// or you update the views' frame.origin.y
// you may want to do the updates within a 0.25s animation
}
}
@objc private func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? CGRect {
// reset views to their original position on keyboard dismiss
}
}
您可能还想 enable/disable 自动工具栏,因为它可能会使您的键盘高度不稳定。
// in viewDidLoad set to false, in deinit set back to true (if you need it)
IQKeyboardManager.shared.enableAutoToolbar = false
我正在使用 IQKeyboardManager
让文本字段在使用键盘输入后保持上升。
即使单击文本字段,我也不想滚动到特定视图。下面是设计的截图。我希望 'header' 保持在顶部。
根据他们的文档,there is a way to keep the navigation bar remain on top.
为您的 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”视图保持在顶部,
这样做:
**
YourViewController.view -> [headerView][contentView]
**
将 textfield 放入 [contentView] 并更改 [contentView].y 而不是上面代码中的 Self.view。
为您的 viewController
禁用 IQKeyboardManager
:
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)
}
@Wolverine 和@Bhavin Ramani 的回答很棒:让您的 自定义 header 保持在顶部的最佳方法是手动操作键盘(根据IQKeyboardSwift 作者comment)。如果您使用 iOS 默认导航栏,它似乎是由图书馆为您处理的。
在这里我只想分享一些关于这个主题的更新,以供我将来参考,因为答案有点旧而且一些 Swift 语法已经改变。下面的代码是用 Xcode 13.2 编写的,针对 iOS 13+.
首先,您想通过
禁用 KQKeyboardManagerIQKeyboardManager.shared.enable = false
请注意,此行仅禁用 向上移动文本字段功能 ,其他 IQKeyboard 功能,如在外部触摸时退出、自动工具栏等,不会因此而禁用线,这通常是你想要的。
然后,您在视图控制器的 viewDidLoad
中注册键盘事件观察器,在 deinit
中删除观察器。
override func viewDidLoad() {
super.viewDidLoad()
IQKeyboardManager.shared.enable = false
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillShow),
name: UIResponder.keyboardWillShowNotification,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillHide),
name: UIResponder.keyboardWillHideNotification,
object: nil)
}
deinit {
IQKeyboardManager.shared.enable = true
NotificationCenter.default.removeObserver(self)
}
接下来,为键盘 show/hide 添加视图移动 up/down 方法。
@objc private func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect {
print("keyboardSize.height", keyboardSize.height)
// using the right key here is important, because
// keyboardFrameEndUserInfoKey is an user info key
// to retrieve the keyboard’s frame at the END of its animation.
// here you move up the views you need to move up
// if you use auto layout, update the corresponding constraints
// or you update the views' frame.origin.y
// you may want to do the updates within a 0.25s animation
}
}
@objc private func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? CGRect {
// reset views to their original position on keyboard dismiss
}
}
您可能还想 enable/disable 自动工具栏,因为它可能会使您的键盘高度不稳定。
// in viewDidLoad set to false, in deinit set back to true (if you need it)
IQKeyboardManager.shared.enableAutoToolbar = false