監聽當鍵盤將要出現時
OC版
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
監聽當鍵將要退出時
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
swift版
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardShow(noty:)), name: Notification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardHidden(noty:)), name: Notification.Name.UIKeyboardWillHide, object: nil)
OC版
當鍵盤出現
- (void)keyboardWillShow:(NSNotification *)notification
{
//獲取鍵盤的高度
NSDictionary *userInfo = [notification userInfo];
NSValue *value = [userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardRect = [value CGRectValue];
int height = keyboardRect.size.height;
}
當鍵退出
- (void)keyboardWillHide:(NSNotification *)notification
{
//獲取鍵盤的高度
NSDictionary *userInfo = [notification userInfo];
NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [value CGRectValue];
int height = keyboardRect.size.height;
}
swift版
鍵盤彈出
@objc func keyBoardShow(noty: Notification) {
guard let userInfo = noty.userInfo else {return}
let value = userInfo["UIKeyboardFrameBeginUserInfoKey"] as! NSValue
let keyboardRect = value.cgRectValue
let keyboradHeight = keyboardRect.size.height
}
鍵盤退出
@objc func keyBoardShow(noty: Notification) {
guard let userInfo = noty.userInfo else {return}
let value = userInfo["UIKeyboardFrameEndUserInfoKey"] as! NSValue
let keyboardRect = value.cgRectValue
let keyboradHeight = keyboardRect.size.height
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。