目中经常遇到键盘出现后,页面上移的问题,所以一并整理在此处,供大家参考。
1. 键盘重要的两个系统通知:
UIKeyboardWillShowNotification -> 键盘将要出现时触发,调出中文候选框也会触发
UIKeyboardWillHideNotification -> 键盘将要消失时触发
2. 键盘高度值:
iPhone (Portrait)
216
252 (带中文候选框)
iPhone (Landscape)
162
198 (带中文候选框)
iPhone中文候选框大小均为36
iPad (Portrait)
264
318 (带中文候选框)
iPad(Landscape)
352
406 (带中文候选框)
iPad中文候选框大小均为54
3. 实战
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock: ^(NSNotification *note)
{
CGRect keyboardFrame;
[note.userInfo[UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
float keyboardHeight = CGRectGetHeight(keyboardFrame);
NSLog(@"键盘将升起");
NSLog(@"键盘高度:%f",keyboardHeight);
// 页面调整
}];