我有一个UIView
作为子视图添加到UIScrollView
UIView
包含UITextField
和UITextView
,以避免键盘隐藏字段我已经注册了keyboardWasShown
和keyboardWillHide
通知,我已经编写了这段代码
- (void)keyboardWasShown:(NSNotification *)notification
{
isKeyboardUp = YES;
// Step 1: Get the size of the keyboard.
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
CGRect TextFieldFrame;
if(activeTextField)
{
TextFieldFrame = [activeTextField.superview convertRect:activeTextField.frame toView:self.view];
}
else
{
TextFieldFrame = [activeTextView.superview convertRect:activeTextView.frame toView:self.view];
}
[self.scrollView scrollRectToVisible:TextFieldFrame animated:YES];
}
- (void) keyboardWillHide:(NSNotification *)notification
{
isKeyboardUp = NO;
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}
此处activeTextField
和activeTextView
是当前使用的文本字段,这在大多数情况下效果很好,但如果实施中出现任何问题,可能会有人建议。
答案 0 :(得分:0)
是的,如果您使用的是scrollview,那么您可以简单地使用TPKEYBOARDTYPING
它可以在可可豆荚上买到,或者你可以从github自己获得它。
添加其文件然后只需转到故事板并在scrollview中将其类更改为tpkeyboardtypingscrollview。
它将在返回时提供所有必要的内容,例如Resign键盘,并将更改scrollView本身的偏移量,以便您的textFields不会隐藏在键盘后面。
答案 1 :(得分:0)
试试这个
-(void)keyboardWillShow:(NSNotification*)notification {
NSDictionary *info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
keyboardHeight = kbSize.height;
[self updateScrollViewPosition];
}
-(void)keyboardDidChange:(NSNotification *)notification {
NSDictionary *info = [notification userInfo];
CGSize kbSizeBegin = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGSize kbSizeEnd = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
if (kbSizeBegin.height != kbSizeEnd.height) {
keyboardHeight = kbSizeEnd.height;
if (activeTextField && [activeTextField isFirstResponder]) {
[self updateScrollViewPosition];
}
}
}
-(void)keyboardWillHide:(NSNotification*)notification {
keyboardHeight = 0;
activeTextField = nil;
[self resignAllTextFields];
}
#pragma mark - UITextFieldDelegate Methods
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
activeTextField = textField;
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
activeTextField = textField;
[self updateScrollViewPosition];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
keyboardHeight = 0;
activeTextField = nil;
[textField resignFirstResponder];
return YES;
}
#pragma mark - Update Method
-(void)updateScrollViewPosition {
if (keyboardHeight > 0 && activeTextField) {
CGRect frame = activeTextField.frame;
CGFloat yPoint = scrollView.frame.origin.y+frame.origin.y+frame.size.height+8.0;
CGFloat height = self.view.frame.size.height-keyboardHeight;
CGFloat diff = yPoint-height;
if (diff > 0.0) {
[scrollView setContentOffset:CGPointMake(0, diff) animated:YES];
}
else {
CGFloat diff = scrollView.contentSize.height-scrollView.contentOffset.y;
if (diff<scrollView.frame.size.height) {
diff = scrollView.contentSize.height-scrollView.frame.size.height;
if (diff < 0) {
diff = 0.0;
}
[scrollView setContentOffset:CGPointMake(0, diff) animated:YES];
}
}
}
else {
CGFloat diff = scrollView.contentSize.height-scrollView.contentOffset.y;
if (diff<scrollView.frame.si ze.height) {
diff = scrollView.contentSize.height-scrollView.frame.size.height;
if (diff < 0) {
diff = 0.0;
}
[scrollView setContentOffset:CGPointMake(0, diff) animated:YES];
}
}
}
辞职
-(void)resignAllTextFields {
for (UIView *view in containerView.subviews) {
if ([view isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField*)view;
[textField resignFirstResponder];
}
}
[self updateScrollViewPosition];
}