ios - 键盘通知

标签 ios swift nsnotificationcenter uikeyboard custom-keyboard

我正在尝试使用键盘扩展从 userInfo 获取键盘大小。

我加

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardDidShowNotification, object: nil)

在我的 KeyboardViewController.swift 中,但是 keyboardWillShow 永远不会被调用。

最佳答案

先注册键盘通知

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    let notificationCenter = NSNotificationCenter.defaultCenter()
    notificationCenter.addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
    notificationCenter.addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}

override func viewDidDisappear(animated: Bool) {
    super.viewDidDisappear(animated)

    let notificationCenter = NSNotificationCenter.defaultCenter()
    notificationCenter.removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
    notificationCenter.removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}

然后

func keyboardWillShow(notification: NSNotification) {
    let userInfo = notification.userInfo!
    let keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().height
}

并且您将高度值作为 CGFloat

关于ios - 键盘通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28129041/

相关文章:

ios - 以编程方式添加的按钮在 ios 中不显示触摸感

ios - 在 Xcode iOS SDK 中读取 SQLite

ios - 来自 "24:00"字符串的 DateFormatter 返回 Nil

ios - UIApplicationDidBecomeActiveNotification不调用

ios - 从导航栏 locationButton 更新所有 View 中的当前位置

iphone - iCloud - 移动文件完成

iOS - NSFetchRequest : sorting for string length

xcode - 使用未解析的标识符 'objects'

ios - 什么时候应该屏蔽 UITableView 部分单元格的角?

macos - 在swift中添加通知观察者