【问题标题】: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

【问题讨论】:

    标签: xcode xcode6 xcode4


    【解决方案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 组件来解决键盘隐藏字段的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      • 2014-11-23
      • 2018-05-21
      • 2010-12-30
      相关资源
      最近更新 更多