键盘上的NSNotificationCenter Swift 3.0显示和隐藏

Dhw*_*eri 15 keyboard-events nsnotificationcenter ios swift

我试图在键盘显示和消失时运行一个函数,并具有以下代码:

let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(ViewController.keyBoardUp(Notification :)), name:  NSNotification.Name.UIKeyboardWillShow, object: nil)
Run Code Online (Sandbox Code Playgroud)

以下功能keyBoardUp:

func keyBoardUp( Notification: NSNotification){
    print("HELLO")
}
Run Code Online (Sandbox Code Playgroud)

但是,当键盘显示时,该功能不会打印到控制台.非常感谢帮助

Van*_*ana 32

斯威夫特3:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: Notification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: Notification.Name.UIKeyboardWillHide, object: nil)

}

func keyboardWillShow(notification: Notification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        print("notification: Keyboard will show")
        if self.view.frame.origin.y == 0{
            self.view.frame.origin.y -= keyboardSize.height
        }
    }

}

func keyboardWillHide(notification: Notification) {
    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)


Rak*_*tri 27

Swift 4.2+

@ vandana的答案已更新,以反映Swift 4.2中对本机通知的更改.

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)

}

@objc func keyboardWillShow(notification: Notification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        print("notification: Keyboard will show")
        if self.view.frame.origin.y == 0{
            self.view.frame.origin.y -= keyboardSize.height
        }
    }

}

@objc func keyboardWillHide(notification: Notification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y != 0 {
            self.view.frame.origin.y += keyboardSize.height
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,您需要使用UIKeyboardFrameEndUserInfoKey来考虑iOS 11safeAreaInset引入的更改.


Pan*_* K. 5

设置键盘通知观察器

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardNotification(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
}
Run Code Online (Sandbox Code Playgroud)

并在您的函数中处理它

func keyboardNotification(notification: NSNotification) {
  print("keyboard displayed!!")
}
Run Code Online (Sandbox Code Playgroud)

希望这会帮助你.


bla*_*acx 5

斯威夫特 4.X/5

我喜欢内联的、基于块的方法,它已经存在很长时间了!addObserver(...) 您可以在此处阅读有关参数的更多信息。

这种方法的一些优点是:

  • 你不需要使用@objc关键字
  • 您在设置观察者的同一位置编写回调代码

重要提示:NotificationCenter.default.removeObserver(observer)在您设置的对象中调用deinit注册观察者(通常是视图控制器)。

let center = NotificationCenter.default

let keyboardWillShowObserver: NSObjectProtocol = center.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: nil) { (notification) in
    guard let value = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
    let height = value.cgRectValue.height

    // use the height of the keyboard to layout your UI so the prt currently in
    // foxus remains visible
}

let keyboardWillHideObserver: NSObjectProtocol = center.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: nil) { (notification) in

    // restore the layout of your UI before the keyboard has been shown
}
Run Code Online (Sandbox Code Playgroud)