我用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 ”视图保持在顶部,
这样做:
**
YourViewController.view -> [headerView][contentView]
**
将文本字段放入 [contentView] 并在上面的代码中更改 [contentView].y 而不是 Self.view。
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)
}
@Wolverine 和 @Bhavin Ramani 的回答很棒:让自定义标题保持在顶部的最佳方法是手动处理键盘(根据 IQKeyboardSwift 作者的评论)。如果您使用 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)
}
接下来,为键盘显示/隐藏添加视图向上/向下移动方法。
@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
}
}
您可能还想启用/禁用自动工具栏,因为它可能会使您的键盘高度不稳定。
// in viewDidLoad set to false, in deinit set back to true (if you need it)
IQKeyboardManager.shared.enableAutoToolbar = false