簡體   English   中英

Swift 5, 如何從鍵盤獲取通知

[英]Swift 5, How do I get notifications from the keyboard

我已經看到其他人從 NotificationCenter 獲得了 swift 4.2 中的 method.userInfo ......我的問題是我如何找到 swift 5.2 中的 userInfo ......它告訴我什么時候能走到這一步

  @objc func keyboardWillShow(notification: NotificationCenter) {
if let keyboardsize = (notification.userInfo?)
}

那個.userInfo? 不是 NotificationCenter 的一種方法...那么,如果我無法從鍵盤上獲取諸如高度之類的信息,如何使我的視圖適應鍵盤

首先,我假設您正在收聽鍵盤通知事件,即Notification.Name.UIKeyboardWillShow

其次,它不是NotificationCenter類型,而是Notification類型

要獲取特定的鍵盤框架,您可以使用UIKeyboardFrameEndUserINfoKey

有關此枚舉的更多信息,請點擊此處

關於如何在通知的 userInfo 中獲取框架的示例


  @objc
    func keyboardWillShow(notification: Notification) {
        if let keyboardFrame = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
             let keyboardFrame = keyboardFrame.cgRectValue
              //print keyboardFrame.height
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM