UIScrollView中UITextView中的键盘方向

时间:2012-09-14 11:22:25

标签: iphone ios uiscrollview uitextfield uitextview

在我的应用程序中,我使用UIScrollView UITextFieldUITextView,我在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];    
}

任何人都可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:1)

使用textview的委托方法为UITextview 像贝娄一样

-(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];
}

希望这能帮到你..

:)

答案 1 :(得分:0)

您可以尝试强烈推荐的“TPKeyboardAvoidingScrollView”,可从以下网址获取:TPKeyboardAvoiding

答案 2 :(得分:0)

首先添加'.h'文件

in did load

textView.delegate=self;

然后 给出textView的代码

textView.userinteractionEnabled=YES;