Iphone 键盘隐藏,视图仅在第一次尝试时无法正确向下滚动

Iphone 键盘隐藏,视图仅在第一次尝试时无法正确向下滚动,iphone,uiscrollview,uikeyboard,Iphone,Uiscrollview,Uikeyboard,当键盘隐藏时,我希望我的视图向下滚动,问题是第一次视图来自其框架之外(我附加的第一个图像),但如果我第二次隐藏键盘,视图将以正确的方式向下滚动(第二个图) 以下是与本部分相关的代码: - (void)viewDidUnload { [[NSNotificationCenter defaultCenter] removeObserver:self name:UI

当键盘隐藏时,我希望我的视图向下滚动,问题是第一次视图来自其框架之外(我附加的第一个图像),但如果我第二次隐藏键盘,视图将以正确的方式向下滚动(第二个图)

以下是与本部分相关的代码:

    - (void)viewDidUnload
    {
     [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:UIKeyboardWillShowNotification
                                                      object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:UIKeyboardWillHideNotification
                                                      object:nil];

    }

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardDidShow:)
                                                     name:UIKeyboardDidShowNotification
                                                   object:self.view.window];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardDidHide:)
                                                     name:UIKeyboardDidHideNotification
                                                   object:nil];
    }

    - (void)keyboardDidShow:(NSNotification *)notification
    {
        [_scrollView setScrollEnabled:TRUE];
    }

- (void)keyboardDidHide:(NSNotification *)notification
{
    [UIView animateWithDuration:4 animations:^{

      NSDictionary* info = [notification userInfo];
     //---obtain the size of the keyboard---
     NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
     CGRect keyboardRect = [self.view convertRect:[aValue CGRectValue] fromView:nil];
     //---resize the scroll view back to the original size (without keyboard)---
     CGRect viewFrame = [self.view frame];
     viewFrame.size.height += keyboardRect.size.height;
     self.scrollView.frame = viewFrame;
     }];


}