【问题标题】:Keyboard orientation in UITextView in UIScrollViewUIScrollView 中的 UITextView 中的键盘方向
【发布时间】:2012-09-14 11:22:25
【问题描述】:

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

谁能帮我解决这个问题?

【问题讨论】:

    标签: iphone ios uiscrollview uitextfield uitextview


    【解决方案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];
    }
    

    希望对你有所帮助..

    :)

    【讨论】:

    • 也在.h文件中定义UITextFieldDelegate
    • 在这些委托方法-(void)textViewDidBeginEditing:(UITextView *)textView; -(void)textViewDidEndEditing:(UITextView *)textView; 我应该怎么做
    • 您在文本字段中使用的代码所有代码都在这些方法中使用,但只使用关键字“textview”而不是“textfield”
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      首先添加你的“.h”文件

      确实加载了

      textView.delegate=self;
      

      那么 给定 textView 的代码

      textView.userinteractionEnabled=YES;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-24
        • 1970-01-01
        • 2011-07-05
        • 2020-03-19
        • 1970-01-01
        • 1970-01-01
        • 2010-11-30
        • 1970-01-01
        相关资源
        最近更新 更多