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

Gij*_*ese 5 ios swift swift3 iqkeyboardmanager

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

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

视图的检查器布局

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

Wol*_*ine 4

禁用 ViewController 的 IQKeyboardManager。

为了那个原因,

IQKeyboardManager.sharedManager().disableInViewControllerClass(ViewController.self)
Run Code Online (Sandbox Code Playgroud)

并在该 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
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

现在你希望你的“ HEADER ”视图保持在顶部,

这样做:

**

YourViewController.view -> [headerView][contentView]

**

文本字段放入 [contentView] 中并更改 [contentView].y 而不是上面代码中的 Self.view。