您的位置:首页 > 其它

获取键盘的高度

2015-07-22 14:21 190 查看
- (void)viewDidLoad
{
[super viewDidLoad];

//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];//注意获取键盘的高度的监听方法,应写在控制器里,若自定义的cell中,有textfield,在自定义的cell中写入监听的话,点击键盘时,第一次点击时高度获取的为0 ,之后再点击才获取到键盘的高度(至于为什么现在还没想清楚)

//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];

}


//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
int height = keyboardRect.size.height;
}


//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: