利用观察者 来监听是否弹出键盘
//监听弹出键盘
[[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;
CGRect rect = self.view.frame;
rect.orignal.y = - height;
self.view.frame = rect;
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
CGRect rect = self.view.frame;
rect.orignal.y = - height;
self.view.frame = rect;
}