【问题标题】:swiping with UIScrollView and moving up the view when using keyboard使用键盘时使用 UIScrollView 滑动并向上移动视图
【发布时间】:2013-03-08 09:32:15
【问题描述】:

我正在使用this 示例项目,我想做的是,当您将UITextField 放在视图上时,当UITextField 在键盘下方时,视图会向上移动一点。

我使用了来自this 项目的TPKeyBoardAvoidingScrollView 类。但是,当我单击UITextField 时,视图向上移动,一切都很好,但是当我单击背景时,它不仅恢复正常的屏幕尺寸并关闭键盘,而是返回到第一个视图而不是停留在查看我们当时所处的位置。

还可以在弹出键盘时左右滚动,知道我该如何解决这个问题吗? Here 是我的项目,我把它们都加在一起了。

【问题讨论】:

    标签: ios objective-c xcode uiscrollview uitextfield


    【解决方案1】:

    在 TPKeyboardAvoidingScrollView.m 中有一个方法(keyboardWillHide:),当键盘要隐藏时会调用它。在该方法中,滚动视图的内容偏移设置为 CGPointZero,因此您的滚动视图将到达第一个视图控制器。

    - (void)keyboardWillHide:(NSNotification*)notification {
        _keyboardRect = CGRectZero;
        _keyboardVisible = NO;
    
        // Restore dimensions to prior size
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
        [UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
        self.contentInset = _priorInset;
        //self.contentOffset = CGPointZero;//Replacing this with below line
        self.contentOffset = CGPointMake(self.contentOffset.x, 0);
        [self setScrollIndicatorInsets:self.contentInset];
        _priorInsetSaved = NO;
        [UIView commitAnimations];
    }
    

    在编辑文本字段时停止滚动-

    - (void)keyboardDidShow:(NSNotification*)notification {
        [self setScrollEnabled:NO];
        //existing code
    }
    
    - (void)keyboardWillHide:(NSNotification*)notification {
        [self setScrollEnabled:YES];
        //existing code with modification of content offset
    }
    

    请记住,这可能会影响您使用 TPKeyboardAvoidingScrollView 对象的其他视图。

    【讨论】:

    • 谢谢,当键盘弹出时我怎么能阻止他们左右滑动?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 2015-02-15
    • 2014-11-22
    • 2018-12-16
    • 2011-09-22
    • 1970-01-01
    相关资源
    最近更新 更多