键盘显示时移动特定的UITextField
问题描述:
我按照Apple文档在键盘出现时向上移动文本框。 代码工作正常我的问题是,我需要一个特定的文本字段向另一个移动,而不是执行代码苹果每个我选择的文本字段向上移动...我怎样才能移动一个特定的文本字段,而不是所有?键盘显示时移动特定的UITextField
非常感谢你,我插入下面的代码使用
-(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的委托。
-
执行
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 }
-
现在,来处理键盘隐藏事件,您可以在
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 }
我希望它可以帮助你。