我已经做到这一点,当用户点击文本字段时,视图会略微向上移动,以便在用户键入时字段仍然可见。它工作得很好,但有时在键盘被解除后,而不是返回到原来的位置,视图向下滑得太远(在顶部留下一个小的空白黑条)。有谁知道如何将视图恢复到原来的位置?这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_userField.returnKeyType = UIReturnKeyDone;
[_userField setDelegate:self];
_passField.returnKeyType = UIReturnKeyDone;
[_passField setDelegate:self];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
- (void)dismissKeyboard
{
[_userField resignFirstResponder];
[_passField resignFirstResponder];
}
-(void)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField:textField up:NO];
}
-(void)animateTextField:(UITextField*)textField up:(BOOL)up
{
const int movementDistance = -130; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? movementDistance : -movementDistance);
[UIView beginAnimations: @"animateTextField" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
答案 0 :(得分:0)
我建议你改用键盘通知。这样,即使您有多个字段,它也可以工作,您可以使用键盘动画为变化设置动画,并且您将确切知道由于键盘显示您将丢失多少屏幕区域。
首先,在视图控制器中,注册并取消注册通知:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
然后实现触发通知时调用的两个方法:
- (void)keyboardWillShowNotification:(NSNotification *)notification {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
[UIView setAnimationCurve:(UIViewAnimationCurve)[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]];
[UIView setAnimationBeginsFromCurrentState:YES];
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
[self.mainView adjustContentWithKeyboardHeight:keyboardFrame.size.height];
[UIView commitAnimations];
}
- (void)keyboardWillHideNotification:(NSNotification *)notification {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
[UIView setAnimationCurve:(UIViewAnimationCurve)[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]];
[UIView setAnimationBeginsFromCurrentState:YES];
[self.mainView adjustContentWithKeyboardHeight:0];
[UIView commitAnimations];
}
最后,在您的视图中实施adjustContentWithKeyboardHeight
公共方法。由于在UIView
动画中调用该方法,因此所有更改都将进行动画处理。
如果要直接移动视图(将它们全部放在要移动的容器视图中),请将原始Y位置保留在私有属性中,然后从该属性中减去keyboardHeight减去textField下面的剩余空间,并且将其分配回该字段(或根据自己的喜好设置框架):
CGFloat offsetY = keyboardHeight - (CGRectGetHeight(self.bounds) - CGRectGetMaxY(self.containerView.frame));
CGRect containerFrame = self.containerView.frame;
containerFrame.origin.y = self.containerViewPositionY - offsetY;
self.containerView.frame = containerFrame;