繁体   English   中英

在 IQKeyboardManager 中始终保持视图在顶部(不要使用键盘滚动)

[英]Keep a view always on top (Don't scroll with keyboard) in IQKeyboardManager

在使用键盘键入后,我正在使用IQKeyboardManager将文本字段保持为 go。

即使单击文本字段,我也不想滚动到特定视图。 下面是设计的截图。 我希望“标题”保持在顶部。

视图的检查器布局

从他们的文档中, 有一种方法可以使导航栏保持在顶部。

为您的 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。

禁用IQKeyboardManagerviewController

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 作者的评论)。 如果您使用 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM