0

在我的应用程序中,我使用了UIScrollViewUITextFieldUITextView我在 UITextField 中对键盘方向进行了编码。它工作正常,但不适用于UITextView. 在编辑时UITextView,键盘完全隐藏了UITextView. 这是我的代码

- (void)textFieldDidBeginEditing:(UITextField *)textField {
  [textField setClearButtonMode:UITextFieldViewModeWhileEditing];
  CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
  CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
  CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
  CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
  CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
  CGFloat heightFraction = numerator / denominator;
  if (heightFraction < 0.0) {
    heightFraction = 0.0;
  } else if (heightFraction > 1.0) {
    heightFraction = 1.0;
  }
  UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];
  if (orientation == UIInterfaceOrientationPortrait ||
    orientation == UIInterfaceOrientationPortraitUpsideDown) {
    animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
  } else {
    animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
  }
  CGRect viewFrame = self.view.frame;
  viewFrame.origin.y -= animatedDistance;
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationBeginsFromCurrentState:YES];
  [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
  [self.view setFrame:viewFrame];
  [UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
  CGRect viewFrame = self.view.frame;
  viewFrame.origin.y += animatedDistance;
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationBeginsFromCurrentState:YES];
  [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
  [self.view setFrame:viewFrame];
  [UIView commitAnimations];    
}

任何人都可以帮我清除这个吗?

4

3 回答 3

1

对 UITextview 使用 textview 的委托方法,如下所示

-(void)textViewDidBeginEditing:(UITextView *)textView;

并且

-(void)textViewDidEndEditing:(UITextView *)textView;

与 textField 委托方法相同

例如 ...

-(void)textViewDidBeginEditing:(UITextView *)textView{

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    self.view.frame=CGRectMake(0, -100, 320, 460);//just for an example
    [UIView commitAnimations];

}

-(void)textViewDidEndEditing:(UITextView *)textView{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    self.view.frame=CGRectMake(0,0, 320, 460);
    [UIView commitAnimations];
}

希望这对你有帮助..

:)

于 2012-09-14T11:32:11.753 回答
0

您可以尝试强烈推荐的“TPKeyboardAvoidingScrollView”,可从:TPKeyboardAvoiding

于 2012-09-14T11:30:50.663 回答
0

首先添加你的'.h'文件

在没有加载

textView.delegate=self;

然后给出 textView 的代码

textView.userinteractionEnabled=YES;
于 2012-09-14T12:00:36.980 回答