问题描述
我按照Apple文档在键盘出现时向上移动文本字段。
代码工作正常我的问题是我需要将一个特定的文本字段移向另一个,而不是实现代码Apple我选择的每个文本字段都向上移动...我该怎么做才能移动特定的textField和不是全部吗?
非常感谢,我插入以下代码
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown :)
name:UIKeyboardDidShowNotification object :零];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden :)
name:UIKeyboardWillHideNotification object:nil];
}
//在发送UIKeyboardDidShowNotification时调用。
- (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];
}
//在发送UIKeyboardWillHideNotification时调用
- (void)keyboardWillBeHidden:(NSNotification *)aNotification {
[ scrollView setContentOffset:CGPointZero animated:YES];
}
您可以通过以下方式实现您的功能执行 textFieldDidBeginEditing
键盘打开文本字段时将调用的方法。因此,您可以在此方法中更改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 =您要垂直向上移动的文字字段
}
现在,要处理键盘隐藏事件,您可以在 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 =您要垂直向下移动的文字字段
}
我希望它可以帮到你。
I followed the Apple documentation to move a textfield upwards when the keypad appears.The code works fine my problem is that I need that one specific textfield is moved towards the other, instead of implementing the code Apple every textfield I select is moved upwards ... How can I do to move a specific textField and not all?
Thank you very much, I insert the following code used
-(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];
}
You can achieve your functionality by following steps.
- Set delegate of your UITextField.
Implement
textFieldDidBeginEditing
method which will be called when keyboard open for textfield. So you may change frame of textfield in this method as below.-(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 }
Now, to handle keyboard hiding event, you can set frame of your textfield to its origin in
textFieldDidEndEditing
method as below.- (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 }
I hope it may help you.
这篇关于键盘显示时移动特定的UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!