【问题标题】:how to scroll down the view when keyboard is hidden隐藏键盘时如何向下滚动视图
【发布时间】:2013-10-07 16:06:06
【问题描述】:

我有一个包含文本字段的视图。还将键盘配置为已完成按钮以隐藏键盘。我已将委托附加到我的 txtfield 以在显示键盘时向上滚动。它工作正常,但是当我点击完成隐藏键盘时,视图仍然向上滚动,用户必须手动将其向后滚动..任何想法/指针如何解决这个问题..

下面是我实现向上滚动功能的代码..

- (void)viewWillAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    keyboardIsShown = NO;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:self.view.window];
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:self.view.window];
}

- (void)viewWillDisappear:(BOOL)animated
{

    [super viewDidDisappear:animated];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillShowNotification
                                                  object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
}


- (void)keyboardWillShow:(NSNotification *)n
{
    if (keyboardIsShown) {
        return;
    }

    NSDictionary* userInfo = [n userInfo];

    // get the size of the keyboard
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    // resize the noteView
    CGRect viewFrame = self.scrollView.frame;
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
    viewFrame.size.height -= (keyboardSize.height - kTabBarHeight);

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    // The kKeyboardAnimationDuration I am using is 0.3
    [UIView setAnimationDuration:kKeyboardAnimationDuration];
    [self.scrollView setFrame:viewFrame];
    [UIView commitAnimations];

    scrollView.contentSize = formView.frame.size;


    keyboardIsShown = YES;
}

- (void)keyboardWillHide:(NSNotification *)n
{
    NSDictionary* userInfo = [n userInfo];

    // get the size of the keyboard
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;


    // resize the scrollview
    CGRect viewFrame = self.scrollView.frame;
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
    viewFrame.size.height += (keyboardSize.height - kTabBarHeight);

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    // The kKeyboardAnimationDuration I am using is 0.3
    [UIView setAnimationDuration:kKeyboardAnimationDuration];
    [self.scrollView setFrame:viewFrame];
    [UIView commitAnimations];

    keyboardIsShown = NO;
}

【问题讨论】:

  • 您可以通过删除所有已注册的观察者来缩短代码:[[NSNotificationCenter defaultCenter] removeObserver:self];

标签: iphone ios objective-c


【解决方案1】:

如果您只想通过动画滚动回顶部。

-(void)keyboardWillHide:(NSNotification *)notification {
        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        CGPoint top = CGPointMake(0, 0);
        [self.scrollView setContentOffset:top animated:YES];
        self.scrollView.scrollIndicatorInsets = contentInsets;
}

上述实现中不需要keyboardIsShown 实例变量

【讨论】:

  • 谢谢@kyle C.. 我添加了 CGPoint top = CGPointMake(0, -65);及其工作。希望这个 Y 坐标的硬编码不会产生任何问题??任何建议请。
【解决方案2】:

试试这个:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, 216, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
    CGRect frame = CGRectMake(0, textFieds.frame.origin.y + kOtherViewsHeightWhichMightNotBeInSameViewAsScrollView, 10, 10);

    [self.scrollView scrollRectToVisible:frame] animated:YES];
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
    return YES;
}

不要忘记将视图控制器设置为文本字段的委托

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多