4

複数のテキスト フィールドがあり、テキスト ボックスにフォーカスすると、自動的に上にスクロールし、キーボードがテキスト フィールドを非表示にします。

クリックしたときにテキストフィールドをフォーカスフィールドにスクロールする方法はありますか?

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

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

}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    activeField = nil;
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    // 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;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
        [scrollView setContentOffset:scrollPoint animated:YES];
    }
}
4

1 に答える 1

5

The code snippet that you posted I think is the one from the Apple's documentation, which assumes a basic view hierarchy with a UIScrollView (or one of its subclasses, like UITableView) filling the entire screen. If your view layout is more complex, or you need to support multiple orientations, the text field won't scroll to visible because rectangle calculations will be wrong. You need to tweak the code a bit and my suggestion is that you approach the problem this way:

The new contentInsent height for your scroll view should be equal to the height of the intersection rectangle between the keyboard and your scroll view.

In code:

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    CGRect kbRawRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];    
    CGRect scrollViewFrame = [self.scrollView.window convertRect:self.scrollView.frame fromView:self.scrollView.superview];

    // Calculate the area that is covered by the keyboard
    CGRect coveredFrame = CGRectIntersection(scrollViewFrame, kbRawRect);
    // Convert again to window coordinates to take rotations into account
    coveredFrame = [self.scrollView.window convertRect:self.scrollView.frame fromView:self.scrollView.superview];

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, coveredFrame.size.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    CGRect activeFieldRect = [self.activeField convertRect:self.activeField.bounds toView:self.scrollView];
   [self.scrollView scrollRectToVisible:activeFieldRect animated:YES];
}

Notice that I've used the convenient UIScrollView's scrollRectToVisible function to abstract the final scrolling operation as much as possible.

于 2013-01-05T01:16:58.553 に答える