我有一个 UITableView
,我想在其中显示键盘。这隐藏了我的最后一行,因此我想获取键盘框架,然后计算单元格的大小。
如何在 Swift 中以编程方式获取键盘的框架?
最佳答案
您需要在 View Controller 中收听 UIResponder.keyboardWillChangeFrameNotification
通知:
let observer = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillChangeFrameNotification, object: nil, queue: nil) { notification in
let frame = notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect
// do something with the frame
}
并且不要忘记稍后删除观察者,这样它就不会导致任何内存问题(例如强引用循环甚至崩溃):
NotificationCenter.default.removeObserver(observer)
关于ios - 以编程方式在 Swift 中获取键盘框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25851626/