我想要显示键盘UITableView
。这是隐藏我的最后一行,因此我想获得键盘框架,然后计算单元格的大小。
如何在Swift中以编程方式获取键盘的框架?
答案 0 :(得分:12)
只需在UIKeyboardWillShowNotification
UIViewController
// in your viewDidLoad:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification)
// later in your class:
func keyboardWillShow(notification: NSNotification) {
let frame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
// do stuff with the frame...
}
答案 1 :(得分:8)
在Swift 2.0中,使用以下内容在viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "keyboardWillShow:",
name: UIKeyboardWillShowNotification,
object: nil
)
第二种方法仍适用于Swift 2.0