1

模式对话框在键盘出现时向上移动,在键盘消失时向下移动。

一切都很好,直到我旋转 iPad。在除标准之外的任何其他方向上它都不起作用。当 iPad 转动时,模式对话框在键盘出现而不是向上移动时向下移动,当键盘消失而不是向下移动时向上移动。

这是我用来在键盘出现/消失时定位模式对话框的代码。

- (void)textFieldDidBeginEditing:(UITextField *)textField {


        self.view.superview.frame = CGRectMake(self.view.superview.frame.origin.x, 140, self.view.superview.frame.size.width, self.view.superview.frame.size.height);

        }      
    }];

}


-(void)textFieldDidEndEditing:(UITextField *)textField {

    [UIView animateWithDuration:0.4 animations:^ {

        self.view.superview.frame = CGRectMake(self.view.superview.frame.origin.x, 212, self.view.superview.frame.size.width, self.view.superview.frame.size.height);
        }
    }];

}
4

2 回答 2

1

不用设置框架,而是使用 CGAffineTransformTranslate,例如像这样:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.view.superview.transform = CGAffineTransformTranslate(self.view.superview.transform,0,72);
    }      
}];
}


-(void)textFieldDidEndEditing:(UITextField *)textField {
[UIView animateWithDuration:0.4 animations:^ {
    self.view.superview.transform = CGAffineTransformTranslate(self.view.superview.transform,0,-72);
    }
}];
}
于 2013-04-19T13:17:48.443 回答
0

您应该尝试使用键盘通知:

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

然后在选择器中调整框架。不在 textFieldDidBeginEditing/textFieldDidEndEditing 中。

- (void)keyboardWasShown:(NSNotification *) notification {
    NSDictionary *info = [notification userInfo];
    NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;
    keyboardHeight = MIN(keyboardSize.height, keyboardSize.width);
    // set new frame based on keyboard size 

- (void)keyboardWillBeDismissed: (NSNotification *) notification{
    [UIView animateWithDuration:0.4 animations:^{
    // original frame 
    }];
}
于 2013-04-19T13:04:52.363 回答