gpt4 book ai didi

ios - 键盘通知

转载 作者:可可西里 更新时间:2023-10-31 23:59:24 26 4
gpt4 key购买 nike

我正在尝试使用键盘扩展从 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com