显示键盘时向上或向下移动视图

Posted

技术标签:

【中文标题】显示键盘时向上或向下移动视图【英文标题】:Moving a View up or Down when a Keyboard is Shown 【发布时间】:2018-12-16 08:22:47 【问题描述】:
   NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name:NSNotification.Name.UIKeyboardWillShow, object: self.view.window)
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide(_:)), name:NSNotification.Name.UIKeyboardWillHide, object: self.view.window)


override func viewWillDisappear(_ animated: Bool) 
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: self.view.window)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: self.view.window)


    func keyboardWillHide(_ sender: Notification) 
    if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue 
    if self.view.frame.origin.y != 0
    self.view.frame.origin.y += keyboardSize.height



    func keyboardWillShow(_ sender: NSNotification) 
        if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey]! as? NSValue)?.cgRectValue 
        if let offset = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey]! as? NSValue)?.cgRectValue 
            if self.view.frame.origin.y == 0
                self.view.frame.origin.y -= keyboardSize.height

        if keyboardSize.height == offset.height 
        if self.view.frame.origin.y == 0 
            UIView.animate(withDuration: 0.15, animations: 
                self.view.frame.origin.y -= keyboardSize.height
)
        
    
    else 
        UIView.animate(withDuration: 0.15, animations: 
            self.view.frame.origin.y += keyboardSize.height - offset.height
            )
    

它不起作用。它一直显示错误“类型'ViewController'没有成员'keyboardWillShow'你的意思是'keyboardWillHideenter image description here

【问题讨论】:

【参考方案1】:

您的代码存在一些问题。首先方法keyboardWillShow和keyboardWillHide应该暴露给Objective-c,其次,你的打开/关闭花括号搞砸了。

我对上面的代码做了一些修改,试试这样:

@objc func keyboardWillHide(_ sender: Notification) 
    if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue 
        if self.view.frame.origin.y != 0 
            self.view.frame.origin.y += keyboardSize.height
        
    


@objc func keyboardWillShow(_ sender: NSNotification) 
    if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey]! as? NSValue)?.cgRectValue,
        let offset = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey]! as? NSValue)?.cgRectValue 

        if self.view.frame.origin.y == 0 

            self.view.frame.origin.y -= keyboardSize.height

            if keyboardSize.height == offset.height 
                if self.view.frame.origin.y == 0 
                    UIView.animate(withDuration: 0.15, animations: 
                        self.view.frame.origin.y -= keyboardSize.height
                    )
                
            
            else 
                UIView.animate(withDuration: 0.15, animations: 
                    self.view.frame.origin.y += keyboardSize.height - offset.height
                )
            
        

    

您可能还想放弃这个 altogeter 并使用 IHKeyboardAvoiding 组件来解决键盘隐藏字段的问题。

【讨论】:

以上是关于显示键盘时向上或向下移动视图的主要内容,如果未能解决你的问题,请参考以下文章

关闭键盘时,视图在 iPhone X 中不断向下移动

InputAccessoryView 使用键盘交互显示/隐藏上下移动表格视图

不希望模态视图在键盘显示上向上移动

如何通过在焦点集中时将其移动到键盘顶部/视图中心来突出显示滚动视图中的焦点 UITextField

其他应用程序的键盘调用keyboardWillShow

键盘出现时如何向上移动集合视图控制器?