移动特定的UITextField时,键盘秀(Move specific UITextField wh

2019-10-22 07:26发布

我跟着苹果文档的键盘出现时向上移动一个文本框。 该代码工作正常,我的问题是不是实现苹果每文本字段我选择向上移动的代码,我需要一个特定的文本框朝对方移动,......我该怎么做才能移动特定的文本框,而不是全部?

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

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

Answer 1:

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

  1. 您的UITextField的设定委托。
  2. 实现textFieldDidBeginEditing方法,将被调用时,键盘打开文本框。 所以,你可以在下面这个方法改变文本框的边框。

     -(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 } 
  3. 现在,来处理键盘隐藏事件,您可以设置文本框的边框到其原籍textFieldDidEndEditing如下方法。

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

我希望它可以帮助你。



文章来源: Move specific UITextField when the Keyboard show