Прокрутка клавиатуры в активном текстовом поле - прокрутка до вне поля зрения?

У меня есть приложение с представлением, в котором есть текстовые поля от верха до низа представления. Мне нужно, чтобы он прокручивался при редактировании нижних полей, чтобы поля были видны, но, похоже, он работает некорректно.

Следуя документам Apple , я поместил весь этот код в свою программу ( Листинги 4-1, 4-2) и добавил выходы scrollView и activeField в мой файл заголовка и связал их с IB.

Проблема в том, что как только я нажимаю на текстовое поле, ВСЕ текстовые поля исчезают из поля зрения, пока я не закрою клавиатуру. Они прокручиваются очень далеко (опять же, достаточно далеко, чтобы ни одно из полей не было видно).

Кто-нибудь знает, чем может быть вызвана эта проблема?

Я помещаю сюда код из Apple Docs, чтобы вы могли точно увидеть, какой код я использую, не щелкая мышью.

//my .h
    IBOutlet UIScrollView *scrollView;
    IBOutlet UITextField *activeField;

//.m
    // Call this method somewhere in your view controller setup code.
    - (void)registerForKeyboardNotifications
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                selector:@selector(keyboardWasShown:)
                name:UIKeyboardDidShowNotification object:nil];

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

}

// Called when the UIKeyboardDidShowNotification is sent.
- (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];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    activeField = textField;
}

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

7
задан Baub 25 August 2011 в 18:34
поделиться

0 ответов