代码之家  ›  专栏  ›  技术社区  ›  Krunal

'类型'通知。名称'没有成员'keyboardDidShowNotification'

  •  9
  • Krunal  · 技术社区  · 6 年前

    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidShow(notification:)), name: NSNotification.Name.keyboardDidShowNotification, object: nil)
    

    下面的一个在Swift 4上运行良好,但在Swift 4.2上运行不好

    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidShow(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
    

    enter image description here

    苹果文件参考号: NSNotification.Name.keyboardDidShowNotification

    2 回复  |  直到 6 年前
        1
  •  92
  •   Rakesha Shastri    5 年前

    我相信你现在是这样用的。

    NotificationCenter.default.addObserver(
        self, 
        selector: #selector(self.keyboardDidShow(notification:)), 
        name: UIResponder.keyboardDidShowNotification, object: nil) 
    
    /* You can substitute UIResponder with any of it's subclass */
    

    UIResponder doc 作为一个 类型属性 .

        2
  •  5
  •   Daniel    6 年前

    处理swift 4,2

     func bindToKeyboard(){
        NotificationCenter.default.addObserver(self, selector: #selector(UIView.keyboardWillChange(_:)), name:
            UIApplication.keyboardWillChangeFrameNotification
            , object: nil)
    
    
    }
    
        3
  •  4
  •   Badr Bujbara    5 年前

    一些小细节补充了这个答案:

    func setupViews(){
                // Keyboard notification observers
            NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    
            NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil) 
        }
    
    
        @objc func keyboardWillShow(notification: NSNotification) {
            if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
                // Do something
            }
        }
    
        @objc func keyboardWillHide(notification: NSNotification) {
            // Do something
        }
    
        4
  •  4
  •   Paresh Mangukiya    4 年前

    UIResponder.keyboardWillHideNotification 除了使用 NSNotification.Name. SWIFT 5号

    NotificationCenter.default.addObserver(self, selector: #selector(KeyboardLayoutConstraint.keyboardWillShowNotification(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHideNotification(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    

    当我发出命令时,单击keyboardWillShowNotification并选择jump to definition。

    extension UIResponder {
    
        
        public class let keyboardWillShowNotification: NSNotification.Name
    
        public class let keyboardDidShowNotification: NSNotification.Name
    
        public class let keyboardWillHideNotification: NSNotification.Name
    
        public class let keyboardDidHideNotification: NSNotification.Name
    
    }