【问题标题】:keyboard hides textfield for different orientation in ipad键盘隐藏 ipad 中不同方向的文本字段
【发布时间】:2012-01-09 05:34:35
【问题描述】:

在我的 iPad 应用程序中,textViewtextField's 很少。当我点击textField 时,键盘覆盖了textField。所以我正在实现下面的代码来向上移动文本视图。但是在轮换到portraitUpsideDown 时它不能正常工作。它以相反的方向向下滑动屏幕。那么我该如何解决这个问题呢??

-(void) animateTextField: (UITextView *) textField up: (BOOL) up
{
    int txtPosition = (textField.frame.origin.y - 540);
    const int movementDistance = (txtPosition < 0 ? 0 : txtPosition); // tweak as needed
    const float movementDuration = 0.3f; // tweak as needed

    int movement = (up ? -movementDistance : movementDistance);

    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}

-(void)textViewDidBeginEditing:(UITextView *)textField
{
    [self animateTextField: textField up: YES];
}

-(void)textViewDidEndEditing:(UITextView *)textField
{
    [self animateTextField: textField up: NO];
}

-(BOOL)textFieldShouldReturn:(UITextView *)theTextField
{
    [theTextField resignFirstResponder];
    return YES;
}

【问题讨论】:

  • 哎呀....刚才我再次检查了您的问题..它适用于 ipad...所有方向都被接受...抱歉给您错误的信息...
  • @DineshRaja.MaG 那么我该如何解决这个问题呢?

标签: objective-c ios xcode ipad


【解决方案1】:

此解决方案适用于 iPhone,但它同时考虑了两个方向。

你可以稍微适应一下,瞧:

http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html

【讨论】:

    【解决方案2】:

    疯了,

    只需添加另一个功能:

    - (void) animateTextView: (UITextView*) textView up: (BOOL) up
    {
        const int movementDistance = 80; // tweak as needed
        const float movementDuration = 0.3f; // tweak as needed
    
        int movement = (up ? -movementDistance : movementDistance);
    
        [UIView beginAnimations: @"anim" context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        self.view.frame = CGRectOffset(self.view.frame, 0, movement);
        [UIView commitAnimations];
    }
    

    然后这样称呼它:

    - (void)textViewDidBeginEditing:(UITextView *)textView {
        [self animateTextView: textView up: YES];
    }
    
    - (void)textViewDidEndEditing:(UITextView *)textView {
        [self animateTextView: textView up: NO];
    }
    

    【讨论】:

      【解决方案3】:

      如果你的方法是这样的。

      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
      {
          // Return YES for supported orientations
          return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
      }
      

      试试这个。我不确切知道。但我正在努力帮助你。可能是xy 坐标不能在任何方向改变。所以试试这个。

      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
        {
           if(interfaceOrientation=UIInterfaceOrienationPotraitUpsideDown){
                //Declare txtPos globally...
                txtPos=(textField.frame.origin.y + 540);
            }
           if(interfaceOrientation=UIInterfaceOrienationPotrait)
            {
                txtPos=(textField.frame.origin.y - 540);
            }
          return(YES);
        }
      

      在动画方法中。 将textPos 分配给txtPosition 变量..

      【讨论】:

      • 你和上面一样。但是在旋转到 PortraitUpsideDown 时它不起作用。而不是向上移动它向下移动。它只适用于纵向视图而不适用于 PortraitUpSideDown。
      • 除了你的键盘,其他所有东西都将其方向更改为倒置????
      • 是的,其他一切都在颠倒,而不是向上移动屏幕,它向下移动并隐藏 textview ..它在纵向上做相反的事情
      • 您为文本字段 na 添加了 iboutlet...所以您可以将它用于代码中任何位置的任何变量。您只需在接口文件中将“txtPos”声明为整数... .就是这样......
      • 在你的动画方法中只需添加这个“int txtPosition=txtPos”..而不是“txtPosition=(textField.frame.origin.y - 540)
      【解决方案4】:

      您应该使用键盘将显示和隐藏通知来捕获键盘事件并相应地调整您的视图。

      - (void)dealloc {
          [[NSNotificationCenter defaultCenter] removeObserver:self];
      }
      
      - (void)viewDidLoad {
          [super viewDidLoad];
      
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardDidHideNotification object:nil];
      
      }
      
      - (void)keyboardWillShow:(NSNotification *)notification {
          CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
          CGFloat keyboardHeight = CGRectGetHeight(keyboardFrame);
          CGFloat animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
          UIViewAnimationCurve animationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
          UIViewAnimationOptions animationOption = animationCurve << 16;
      
          [UIView animateWithDuration:animationDuration delay:0 options:animationOption animations:^{
              // adjust height using keyboardHeight
          } completion:^(BOOL finished) {
      
          }];
      }
      
      - (void)keyboardWillHide:(NSNotification *)notification {
          CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
          CGFloat keyboardHeight = CGRectGetHeight(keyboardFrame);
          CGFloat animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
          UIViewAnimationCurve animationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
          UIViewAnimationOptions animationOption = animationCurve << 16;
      
          [UIView animateWithDuration:animationDuration delay:0 options:animationOption animations:^{
              // adjust height using keyboardHeight
          } completion:^(BOOL finished) {
      
          }];
      }
      

      这篇博文详细解释了这一点

      http://charlie.cu.cc/2015/10/solution-to-the-ios-software-keyboard-cover-part-of-the-ui/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-18
        • 2011-01-19
        • 1970-01-01
        • 2018-12-22
        • 1970-01-01
        • 2021-05-07
        • 2021-05-20
        • 2023-03-10
        相关资源
        最近更新 更多