0

我在 UIscrollView 中出现键盘问题。

我添加了一个 UIScrollview 作为

scrlView=[[UIScrollView alloc] initWithFrame:CGRectMake(10, 140, 1000, 600)];
scrlView.scrollEnabled=YES;
scrlView.showsVerticalScrollIndicator=YES;
scrlView.bounces=NO;

在此滚动视图中,我添加了 10 行 UITextField,每行有 5 个文本字段,每个文本字段高度为 50 像素。当试图编辑文本字段时,它与键盘重叠。为此,我尝试了这段代码

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


   - (void)keyboardWasShown:(NSNotification*)aNotification
  {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect bkgndRect = selectetTxtfld.superview.frame;
bkgndRect.size.height += kbSize.height;
[selectetTxtfld.superview setFrame:bkgndRect];
[scrlView setContentOffset:CGPointMake(0.0, selectetTxtfld.frame.origin.y) animated:YES];
}

}

// 在发送 UIKeyboardWillHideNotification 时调用

 - (void)keyboardWillBeHidden:(NSNotification*)aNotification
  {
  UIEdgeInsets contentInsets = UIEdgeInsetsZero;

  [UIView animateWithDuration:0.4 animations:^{
     scrlView.contentInset = contentInsets;
  }];
  scrlView.scrollIndicatorInsets = contentInsets;
 }

但是 textField 没有出现在键盘上。它出现在滚动视图的 ypoint 位置

帮我解决这个问题。我在 StackOverFlow 中看到了很多答案。但没有解决我的问题

4

2 回答 2

1

在keyboardWasShown中: 1.在滚动视图底部添加内容插入,其值等于键盘的高度。2. setContentOffset = 当前偏移量+ 键盘高度。注意:1&3 应该在持续时间等于 0.30 的动画块中完成

在keyboardWillBeHidden 中: 1.set contentInset = UIEdgeInsetsZero 2. setContentOffset = 当前偏移量-键盘高度。注意:1&3 应该在持续时间等于 0.30 的动画块中完成

这应该解决你的问题:)

于 2013-10-04T17:58:03.773 回答
1
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {

NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGFloat offSetAfterKeyboardIsDisplayed = scrlview.contentOffset.y + kbSize.height;

[UIView animateWithDuration:0.3 animations:^{
//adding content inset at the bottom of the scrollview
   scrlView.contentInset = UIEdgeInsetMake(0,0,kbSize.height,0);
   [scrlview setContentOffset:offSetAfterKeyboardIsDisplayed]
}];
}


- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{

NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGFloat offSetAfterKeyboardResigns = scrlview.contentOffset.y - kbSize.height;

[UIView animateWithDuration:0.3 animations:^{
   scrlView.contentInset = UIEdgeInsetsZero;
   [scrlview setContentOffset:offSetAfterKeyboardResigns]
}];
}
于 2013-10-04T18:32:33.567 回答