7

表示されている を使用してサイズを変更することによりUIScrollView、キーボードが aを非表示にするときに a を移動しようとしています。UITextFieldcontentInsets

ただし、キーボードの高さでは機能しません。キーボードの高さは 216 ですが、下のインセットを iPhone 縦向きモードの場合は 515、iPhone 横向きモードの場合は 310 に設定すると、正しい位置でスクロールが停止するだけです。なぜこれらの寸法はそれほど異なるのでしょうか? これらの任意の値をハードコードしたくありません。

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.frame = self.parentViewController.view.frame;

    [self.textView becomeFirstResponder];

    NSLog(@"scrollview: %f,%f, parent: %f,%f", self.view.frame.size.height, self.view.frame.size.width, self.parentViewController.view.frame.size.height, self.parentViewController.view.frame.size.width);

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

- (void)keyboardWasShown:(NSNotification *)notification
{
    if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone) {

        // Step 1: Get the size of the keyboard.
        CGFloat keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;

        // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0);
        ((UIScrollView*)self.view).contentInset = contentInsets;
        ((UIScrollView*)self.view).scrollIndicatorInsets = contentInsets;

        // Step 3: Scroll the target text field into view.
        CGRect aRect = self.view.frame;
        aRect.size.height = aRect.size.height - keyboardHeight;
        if (!CGRectContainsPoint(aRect, self.textView.frame.origin) ) {
            CGPoint scrollPoint = CGPointMake(0.0, self.textView.frame.origin.y - keyboardHeight);
            [((UIScrollView*)self.view) setContentOffset:scrollPoint animated:YES];
        }
    }
}

- (void) keyboardWillHide:(NSNotification *)notification {
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    ((UIScrollView*)self.view).contentInset = contentInsets;
    ((UIScrollView*)self.view).scrollIndicatorInsets = contentInsets;
}

編集:

キーボードを開く前に、これを印刷します。

NSLog(@"scrollview: %f,%f, parent: %f,%f", self.view.frame.size.height, self.view.frame.size.width, self.parentViewController.view.frame.size.height, self.parentViewController.view.frame.size.width);

そして、これを出力します:

scrollview: 431.000000,320.000000, parent: 431.000000,320.000000
4

2 に答える 2

23

UIKeyboardFrameEndUserInfoKey主な問題は、の代わりに使用する必要があることだと思いますUIKeyboardFrameBeginUserInfoKey

そうは言っても、スクロールビューが画面の下部に配置されていない場合でも機能するこのメソッドの別のバージョンを作成し、より適切に機能する可能性があります。

self.activeTextFieldの代わりに使用することに注意してくださいself.textField

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    // Calculate the frame of the scrollview, in self.view's coordinate system
    UIScrollView *scrollView    = self.scrollView;
    CGRect scrollViewRect       = [self.view convertRect:scrollView.frame fromView:scrollView.superview];

    // Calculate the frame of the keyboard, in self.view's coordinate system
    NSDictionary* info          = [aNotification userInfo];
    CGRect kbRect               = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    kbRect                      = [self.view convertRect:kbRect fromView:nil];

    // Figure out where the two frames overlap, and set the content offset of the scrollview appropriately
    CGRect hiddenScrollViewRect = CGRectIntersection(scrollViewRect, kbRect);
    if (!CGRectIsNull(hiddenScrollViewRect))
    {
        UIEdgeInsets contentInsets       = UIEdgeInsetsMake(0.0,  0.0, hiddenScrollViewRect.size.height,  0.0);
        scrollView.contentInset          = contentInsets;
        scrollView.scrollIndicatorInsets = contentInsets;
    }

    [self scrollToActiveTextField];
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    self.scrollView.contentInset          = UIEdgeInsetsZero;
    self.scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
}

- (void)scrollToActiveTextField
{
    if (self.activeTextField)
    {
        CGRect visibleRect = self.activeTextField.frame;
        visibleRect        = [self.scrollView convertRect:visibleRect fromView:self.activeTextField.superview];
        visibleRect        = CGRectInset(visibleRect, 0.0f, -5.0f);
        [self.scrollView scrollRectToVisible:visibleRect animated:YES];
    }
}
于 2013-05-29T06:14:46.100 に答える