iphone - UIScrollView 中 UITextView 中的键盘方向

标签 iphone ios uiscrollview uitextfield uitextview

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

谁能帮我解决这个问题吗?

最佳答案

UITextview使用textview的委托(delegate)方法 就像下面这样

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

还有

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

与textField委托(delegate)方法相同

例如...

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

希望这对你有帮助..

:)

关于iphone - UIScrollView 中 UITextView 中的键盘方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12423516/

相关文章:

ios - 更改 ScrollView 中的按钮位置

ios - Swift 中 iOS 应用程序上的 ScrollView

iphone - hideBottomBarWhenPushed 但弹出时

iphone - 为什么有些 ivars 有双下划线前缀?

ios - MapBox GL 从 makefile 构建 Xcode 项目

ios - Oracle MAF 应用程序无法在 iPhone 上加载远程 URL

ios - 向 UIViewController 外观添加淡入淡出动画

iphone - 可以升级到 iOS 6.1.2 sdk 吗?

iphone - 从网络请求中查找 iphone 设备 ID

ios - 在 Swift 中使用 UIScrollView 无限循环滚动