键盘隐藏,视图只有第一次尝试才能正确向下滚动

时间:2013-03-06 04:01:31

标签: iphone uiscrollview uikeyboard

我希望我的视图在键盘隐藏时向下滚动,这是第一次视图来自其框架外部(我已连接的第一个图像),但如果我第二次隐藏键盘,则视图向下滚动以正确的方式(第二个数字)。

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

    - (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;
     }];


}

enter image description here enter image description here

0 个答案:

没有答案