美文网首页
iOS获取键盘高度

iOS获取键盘高度

作者: 8f1557a248b5 | 来源:发表于2016-08-20 00:11 被阅读11次

- (void) registerForKeyboardNotifications{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyBoard:) name:UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyBoard:) name:UIKeyboardDidHideNotification object:nil];

}

- (void)showKeyBoard:(NSNotification *) notification{
NSDictionary *info = [notification userInfo];
NSValue *value = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
CGRect scrollViewFrame= [scrollView frame];
scrollViewFrame.size.height -= keyboardSize.height;
scrollView.frame = scrollViewFrame;
[scrollView scrollRectToVisible:inputElementFrame animated:YES];
isShowKeyboard = YES;
}

- (void)hideKeyBoard:(NSNotification *)notification{
NSDictionary *info = [notification userInfo];
NSValue *value = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
CGRect scrollViewFrame= [scrollView frame];
scrollViewFrame.size.height += keyboardSize.height;
scrollView.frame = scrollViewFrame;
isShowKeyboard = NO;
}

相关文章

网友评论

      本文标题:iOS获取键盘高度

      本文链接:https://www.haomeiwen.com/subject/otxisttx.html