我正在为我的 iOS 应用程序创建一个“登录”和“创建帐户”表单。我成功实现了 UITextField 隐藏时的向上滚动。但是,既然我实现了“下一步”按钮,则不会调用“UIKeyboardDidShowNotification”,因为键盘永远不会被关闭。我需要调用keyboardWasShow 方法,以便检查活动的UITextField 是否隐藏。
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
CGPoint pointInSuperview = [self.view convertPoint:self.activeField.frame.origin fromView:self.scrollView];
aRect.size.height -= kbSize.height;
//added 10 to y axis because tip of origin was outside of keyboard
pointInSuperview.y +=20;
if (!CGRectContainsPoint(aRect, pointInSuperview)) {
CGPoint scrollPoint = CGPointMake(0.0, pointInSuperview.y - (kbSize.height -15));
NSLog(@"it is not in the rect");
[self.scrollView setContentOffset:scrollPoint animated:YES];
}
}
我有一个观察员
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
在我实现了我的 Next 按钮(见下文)之后,不会调用 keyboardWasShown 方法,因此它永远不会检查活动的 UITextField 是否被隐藏。
//functionality for next action
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == self.emailAddress) {
[self.fullName becomeFirstResponder];
[self keyboardWasShown:(NSNotification*)UIKeyboardDidShowNotification];
}
else if (textField == self.fullName) {
[self.password becomeFirstResponder];
}
else if (textField == self.password) {
[self.confirmPassword becomeFirstResponder];
}
[textField resignFirstResponder];
return YES;
}
当用户单击“下一步”按钮时,调用 keyboardWasShown 的最佳方法是什么?我尝试将其设为公共方法,但是当我尝试手动调用它时,我不断收到错误消息。