iOSアプリの「サインイン」および「アカウントの作成」フォームを作成しています。UITextFieldが非表示のときに上にスクロールすることを正常に実装しました。ただし、「次へ」ボタンを実装したので、キーボードが閉じられないため、「UIKeyboardDidShowNotification」は呼び出されません。アクティブなUITextFieldが非表示になっているかどうかを確認できるように、keyboardWasShowメソッドを呼び出す必要があります。
// 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];
[次へ]ボタン(以下を参照)を実装した後、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を呼び出すための最良のアプローチは何でしょうか。パブリックメソッドにしようとしましたが、手動で呼び出そうとするとエラーが発生し続けました。