1

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

这是与这部分相关的代码:

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


}

在此处输入图像描述 在此处输入图像描述

4

0 回答 0