我有多个UITextFields
,我需要在键盘出现时向上移动视图。但我只需要3个底部字段,并且不想在其他字段上移动视图。我使用此代码,但是当用户点击每个字段时它的移动视图,而我只需要在用户点击2个底部字段时移动视图
- (void)viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark - keyboard movements
- (void)keyboardWillShow:(NSNotification *)notification
{
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = -115.0f; //set the -35.0f to your required value
self.view.frame = f;
}];
}
-(void)keyboardWillHide:(NSNotification *)notification
{
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = 0.0f;
self.view.frame = f;
}];
}
答案 0 :(得分:5)
您可以在UITextfield委托方法中更改任何UIViews,UIButton ....的框架textFieldDidBeginEditing:当您点击文本字段时,此委托将调用
- (void)textFieldDidBeginEditing:(UITextField *)textField{
//here you will get the selected textfield
//check whether the textfield is equal to any of three bottom field
if(textfield==BottomTextfield)
{
//here you can change the frame of the UIViews
}
}
答案 1 :(得分:0)
您需要使用UIView's
委托方法更改UITextField's
框架:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
int tag = textField.tag;
int section = tag / 1000;
tag -= 1000 * section;
section -= 1;
CGPoint point = [[textField superview] convertPoint:textField.center toView:self.view];
CGPoint contentOffset = tableView.contentOffset;
if (IOS_7) {
contentOffset.y += (point.y - 230);
// Adjust this value as you need
}
else{
contentOffset.y += (point.y - 150); // Adjust this value as you need
}
currentY = contentOffset.y;
[tableView setContentOffset:contentOffset animated:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[tableView setContentOffset:CGPointMake(0, 0)];
}
希望这有助于...... !!!
答案 2 :(得分:0)
您可以检查第一响应者(活动文本字段)。这个SO答案显示了如何做到这一点:https://stackoverflow.com/a/1823360/2486394。并且仅针对某些文本字段移动视图。