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

时间:2016-10-19 06:49:26

标签: ios swift swift3 iqkeyboardmanager

在使用键盘输入后,我正在使用IQKeyboardManager来保持文本字段的显示。

即使单击文本字段,我也不想滚动到特定视图。以下是设计的屏幕截图。我希望'标题'保持在最顶层。

inspector layout for the view

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

2 个答案:

答案 0 :(得分:2)

试试这个。禁用此viewController的IQKeyboardManager。

为此,

IQKeyboardManager.sharedManager().disableInViewControllerClass(your view controller class name here)

并在那个viewController中编写以下代码。它将根据键盘高度移动您的视图

override func viewDidLoad() {
        super.viewDidLoad()

        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.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] .y而不是Self.view。

答案 1 :(得分:1)

禁用IQKeyboardManager的{​​{1}}:

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
            }
        }
    }