Ios8 键盘显示时移动特定UITextField

Ios8 键盘显示时移动特定UITextField,ios8,keyboard,xcode6,uitextfield,Ios8,Keyboard,Xcode6,Uitextfield,我按照苹果的文档,在键盘出现时向上移动一个文本字段。 代码运行良好我的问题是我需要将一个特定的文本字段移向另一个,而不是实现代码Apple我选择的每个文本字段都向上移动。。。如何移动特定的文本字段而不是全部 非常感谢,我插入下面使用的代码 -(void)viewWillAppear:(BOOL)animated { [[NSNotificationCenter defaultCenter] addObserver:self

我按照苹果的文档,在键盘出现时向上移动一个文本字段。 代码运行良好我的问题是我需要将一个特定的文本字段移向另一个,而不是实现代码Apple我选择的每个文本字段都向上移动。。。如何移动特定的文本字段而不是全部

非常感谢,我插入下面使用的代码

-(void)viewWillAppear:(BOOL)animated 
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];

}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification {
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGRect bkgndRect = changePasswordTextField.superview.frame;
    bkgndRect.size.height -= kbSize.height;
    [scrollView setContentOffset:CGPointMake(0.0, changePasswordTextField.frame.origin.y+kbSize.height) animated:YES];
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {


    [scrollView setContentOffset:CGPointZero animated:YES];
}

您可以通过以下步骤实现您的功能

  • 设置UITextField的委托
  • 实现
    textfielddebeginediting
    方法,当键盘为textfield打开时将调用该方法。因此,您可以按如下方法更改textfield的框架

    -(void)textFieldDidBeginEditing:(UITextField *)textField{
         [textField setFrame:CGRectMake(0.0, textField.frame.origin.y-VALUE,textField.frame.size.width,textField.frame.size.height) animated:YES];
         // VALUE = textfield you want to move upward vertically
    }
    
    - (void)textFieldDidEndEditing:(UITextField *)textField{
          [textField setFrame:CGRectMake(0.0, textField.frame.origin.y+VALUE,textField.frame.size.width,textField.frame.size.height) animated:YES];
          // VALUE = textfield you want to move downward vertically
    }
    
  • 现在,要处理键盘隐藏事件,您可以在
    textfielddidediting
    方法中将textfield的框架设置为其原点,如下所示

    -(void)textFieldDidBeginEditing:(UITextField *)textField{
         [textField setFrame:CGRectMake(0.0, textField.frame.origin.y-VALUE,textField.frame.size.width,textField.frame.size.height) animated:YES];
         // VALUE = textfield you want to move upward vertically
    }
    
    - (void)textFieldDidEndEditing:(UITextField *)textField{
          [textField setFrame:CGRectMake(0.0, textField.frame.origin.y+VALUE,textField.frame.size.width,textField.frame.size.height) animated:YES];
          // VALUE = textfield you want to move downward vertically
    }
    
  • 我希望它能帮助你