您的位置:首页 > 其它

输入框根据键盘显示与隐藏移动位置

2016-03-23 11:08 211 查看
#pragma mark - textFieldDelegate

 - (void)textFieldDidBeginEditing:(UITextField *)textField{
if (textField == _text2) {

[self registerForKeyboardNotifications];
}
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}

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

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

return;
}

- (void)removeForKeyboardNotifications{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *) notif {
NSDictionary *info = [notif userInfo];
NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
[_scrollView setContentOffset:CGPointMake(_scrollView.contentOffset.x,_scrollView.contentOffset.y + keyboardSize.height + 10) animated:YES];

return;
}

- (void)keyboardWillHide:(NSNotification *) notif {
NSDictionary *info = [notif userInfo];
NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
[_scrollView setContentOffset:CGPointMake(_scrollView.contentOffset.x, _scrollView.contentOffset.y - keyboardSize.height - 10)  animated:YES];
return;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: