【问题标题】:Keep a view always on top (Don't scroll with keyboard) in IQKeyboardManager在 IQKeyboardManager 中始终保持视图位于顶部(不要使用键盘滚动)
【发布时间】:2016-10-19 06:49:26
【问题描述】:

我正在使用IQKeyboardManager 让文本字段在使用键盘输入后保持上升。

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

从他们的文档中,there is a way to keep the navigation bar remain on top.

【问题讨论】:

  • 我建议,要实现此功能,请为此视图控制器禁用 IQKeyboardmanager 并在键盘出现时手动将您的文本字段向上移动。
  • 我是新手。这就是我选择这个图书馆的原因。没办法?
  • @GijoVarghese 有 tableview 在视野中吗?
  • @BhavinRamani 是的

标签: ios swift swift3 iqkeyboardmanager


【解决方案1】:

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

【讨论】:

  • 这里的“登录”是什么?
  • @GijoVarghese 将 login 替换为您的 ViewController 名称。
  • 是的。得到它的工作。但问题是,单击文本字段,屏幕上的所有内容都会出现。包括顶部的视图。是否有可能使该视图始终保持在首位?
  • 是的,你可以做到。在您看来,取 2 个视图。 1 是标题。 2. 内容。而不是更改 self.view 框架,而是使用 Content 并更改其框架。
  • 从字面上看,你只是说“不要使用 IQKeyboardManager,自己管理约束。
【解决方案2】:

为您的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)    
    }

【讨论】:

    【解决方案3】:

    @Wolverine 和@Bhavin Ramani 的回答非常棒:让您的自定义标题 保持领先的最佳方式是手动处理您的键盘(根据 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)
        }
    

    接下来,为键盘显示/隐藏添加视图向上/向下移动方法。

        @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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 2012-01-10
      相关资源
      最近更新 更多