ios 如何獲取彈出鍵盤的高度

在很多時候會需要獲取彈出鍵盤的高度來進行動態布局這個時候根據不同的機型大小是不同的這時候就應該動態獲取

實現步驟?

1.利用觀察者 來監聽是否彈出鍵盤

//監聽彈出鍵盤

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(keyboardWillShow:)

name:UIKeyboardWillShowNotification

object:nil];

//可以監聽收回鍵盤

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(keyboardWillHide:)

name:UIKeyboardWillHideNotification

object:nil];

//創建觀察者回調方法

- (void)keyboardWillShow:(NSNotification *)aNotification

{

//創建自帶來獲取穿過來的對象的info配置信息

NSDictionary *userInfo = [aNotification userInfo];

//創建value來獲取 userinfo里的鍵盤frame大小

NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

//創建cgrect 來獲取鍵盤的值

CGRect keyboardRect = [aValue CGRectValue];

//最后獲取高度 寬度也是同理可以獲取

int height = keyboardRect.size.height;

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容