Правильно ли работает UIScrollview с UIStatusbar?

моя проблема связана с UIScrollview. давайте опишем это,

У меня есть экран регистрации с прокруткой, изначально прокрутка не включена. когда появляется клавиатура, я включаю прокрутку, а когда клавиатура снова скрывается, я отключаю прокрутку. ширина и высота моего прокрутки такие же, как у его представления по умолчанию, я применил горизонтальный и вертикальный центр в контейнере, а также верхние, нижние, передние и задние края равны нулю (т.е. они равны представлению по умолчанию). У меня есть кнопка регистрации, которая переходит на экран регистрации и применяет ограничения снизу (ограничения нижнего пространства = 0), также я использую уведомление с клавиатуры для отображения и скрытия.

Актуальная проблема: когда появляется клавиатура с текстовым полем, прокручивается прокрутка, а когда я закрываю клавиатуру, прокрутка опускается, но на этот раз кнопка регистрации будет немного двигаться вверх (например, нижнее пространство имеет ограничения в 20 точек).

В первый раз это похоже на прокрутку, которая начинается после строки состояния, но когда клавиатура появляется и скрывает ее, как прокрутка, она отображается поверх представления, включая строку состояния.

Нужно ли мне добавлять какие-либо ограничения, связанные с Top/Bottom Layout Guide в IB? или мне нужно добавить какие-либо ограничения, связанные с viewDidLoad

Код для уведомления клавиатуры.

-(void)keyboardWillShow:(NSNotification *)notification {
    [self.navigationController.navigationBar setBackgroundImage:nil
                                                  forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = nil;
    self.ContentScrollView.scrollEnabled=YES;
    NSDictionary *userInfo = [notification userInfo];

    CGRect keyboardFrameInWindow;
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrameInWindow];

    // the keyboard frame is specified in window-level coordinates. this calculates the frame as if it were a subview of our view, making it a sibling of the scroll view
    CGRect keyboardFrameInView = [self.ContentScrollView convertRect:keyboardFrameInWindow fromView:nil];

    CGRect scrollViewKeyboardIntersection = CGRectIntersection(self.ContentScrollView.frame, keyboardFrameInView);
    UIEdgeInsets newContentInsets = UIEdgeInsetsMake(0, 0, scrollViewKeyboardIntersection.size.height, 0);

    // this is an old animation method, but the only one that retains compatibility between parameters (duration, curve) and the values contained in the userInfo-Dictionary.
    [UIView animateWithDuration:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue] delay:0.0 options:[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue] animations:^{
        self.ContentScrollView.contentInset = newContentInsets;
        self.ContentScrollView.scrollIndicatorInsets = newContentInsets;

        /*
         * Depending on visual layout, _activeField should either be the input field (UITextField,..) or another element
         * that should be visible, e.g. a purchase button below an amount text field
         * it makes sense to set _activeField in delegates like -textFieldShouldBeginEditing: if you have multiple input fields
         */
        if (_activeField) {
            CGRect controlFrameInScrollView = [self.ContentScrollView convertRect:_activeField.bounds fromView:_activeField]; // if the control is a deep in the hierarchy below the scroll view, this will calculate the frame as if it were a direct subview
            controlFrameInScrollView = CGRectInset(controlFrameInScrollView, 0, 0); // replace 10 with any nice visual offset between control and keyboard or control and top of the scroll view.

            CGFloat controlVisualOffsetToTopOfScrollview = (controlFrameInScrollView.origin.y - self.ContentScrollView.contentOffset.y)+10;
            CGFloat controlVisualBottom = controlVisualOffsetToTopOfScrollview + controlFrameInScrollView.size.height;

            // this is the visible part of the scroll view that is not hidden by the keyboard
            CGFloat scrollViewVisibleHeight = self.ContentScrollView.frame.size.height - scrollViewKeyboardIntersection.size.height;

            if (controlVisualBottom > scrollViewVisibleHeight) { // check if the keyboard will hide the control in question
                // scroll up until the control is in place
                CGPoint newContentOffset = self.ContentScrollView.contentOffset;
                newContentOffset.y += (controlVisualBottom - scrollViewVisibleHeight);

                // make sure we don't set an impossible offset caused by the "nice visual offset"
                // if a control is at the bottom of the scroll view, it will end up just above the keyboard to eliminate scrolling inconsistencies
                CGFloat maxScrollViewHeight = MAX(self.ContentScrollView.frame.size.height, self.ContentScrollView.contentSize.height);
                newContentOffset.y = MIN(newContentOffset.y, maxScrollViewHeight - scrollViewVisibleHeight);
                [self.ContentScrollView setContentOffset:newContentOffset animated:NO]; // animated:NO because we have created our own animation context around this code
            } else if (controlFrameInScrollView.origin.y < self.ContentScrollView.contentOffset.y) {
                // if the control is not fully visible, make it so (useful if the user taps on a partially visible input field
                CGPoint newContentOffset = self.ContentScrollView.contentOffset;
                newContentOffset.y = controlFrameInScrollView.origin.y;

                [self.ContentScrollView setContentOffset:newContentOffset animated:NO]; // animated:NO because we have created our own animation context around this code
            }
        }

    } completion:NULL];
}


- (void)keyboardWillHide:(NSNotification *)notification {

    NSDictionary *userInfo = [notification userInfo];
    [UIView animateWithDuration:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue ] delay:0.01 options:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]intValue] animations:^{
        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        self.ContentScrollView.contentInset = contentInsets;
        self.ContentScrollView.scrollIndicatorInsets = contentInsets;

        CGPoint scrollPoint;
            self.ContentScrollView.scrollEnabled=NO;
            scrollPoint = CGPointMake(0.0, 0.0);

        [self.ContentScrollView setContentOffset:scrollPoint animated:YES];

    } completion:^(BOOL finished){
        __weak typeof(self) weakSelf=self;
        [weakSelf.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        weakSelf.navigationController.navigationBar.shadowImage = [UIImage new];
    }];

}

Изображение Снимок экрана IB

При необходимости я отправлю снимки экрана до и после уведомления клавиатуры.

Спасибо.


person Nasir    schedule 04.05.2015    source источник
comment
напиши self. automaticallyAdjustsScrollViewInsets=NO; в viewDidLoad и попробуй   -  person Nirav Gadhiya    schedule 04.05.2015
comment
Спасибо, это работает правильно, как мне нужно.   -  person Nasir    schedule 04.05.2015
comment
Добро пожаловать. Я ответил на этот вопрос. вы можете принять это, чтобы другие пользователи могли получить от него помощь.   -  person Nirav Gadhiya    schedule 04.05.2015


Ответы (1)


Из Документация

Логическое значение, указывающее, должен ли контроллер представления автоматически настраивать свои вставки представления прокрутки.

Декларация

@property(nonatomic, assign) BOOL automaticallyAdjustsScrollViewInsets

Обсуждение

Значение по умолчанию — YES, что позволяет контроллеру представления настраивать свои вставки представления прокрутки в ответ на области экрана, занимаемые строкой состояния, панелью навигации и панелью инструментов или панелью вкладок. Установите значение НЕТ, если вы хотите самостоятельно управлять настройками вставки представления прокрутки, например, когда в иерархии представлений имеется более одного представления прокрутки.

Напишите self. automaticallyAdjustsScrollViewInsets=NO; в viewDidLoad и попробуйте

person Nirav Gadhiya    schedule 04.05.2015