首页 文章

编辑时TextField附加到键盘 - 如何使ScrollView与编辑前相同?

提问于
浏览
1

我创建了新的Xcode单视图项目 . 在界面构建器中,我添加了一个UIScrollView来覆盖完整视图 . 在这个scrollview上,我添加了一个UITextField . 生成的UI如下所示:
enter image description here

请注意,此时滚动视图不会滚动,因为内容仅采用视图大小而不大于视图大小 .

现在,在编辑时将UITextField置于键盘顶部,我按照Apple在_1541726页面上描述的方式进行操作 . 执行此操作后,它给了我预期的行为,在编辑开始时将文本字段放在键盘上方,如下面的屏幕截图所示:
enter image description here

现在,在调用 [textfield endEditing:YES] 之后,键盘会隐藏自己,但文本字段不会返回其原始位置 . 它返回到原来位置稍微高一点的位置,现在滚动视图变得可滚动,好像它的高度增加了一样:

enter image description here

请注意上面屏幕截图中的滚动条!

我想要帮助在编辑结束后(当键盘隐藏时)恢复视图的原始行为,即textField返回到完全相同的位置并且滚动应该发生,因为它在编辑开始之前没有发生 .

项目网址: - https://github.com/therohansanap/keyboard.git

6 回答

  • 1
    You need adjust scrollview contentOffset textFieldDidBeginEditing and textFieldDidEndEditing.
    or 
    One controller is available for scrollview auto scroll.
    

    https://github.com/simonbs/BSKeyboardControls

  • 0

    我认为Apple here指定的官方方式是保持此功能正常运行的最简单和最佳方式 .

  • 1

    您也可以在不使用键盘通知的情况下执行类似的操作 . 您可能知道我们有 TextField 委托方法,我们可以使用它们来设置scrollView contentOffset并获取相同的行为

    - (void)textFieldDidBeginEditing:(UITextField *)textField{
        scrollView.contentOffset = CGPointMake(0, textField.center.y-80); // you can change 80 to whatever which fits your needs
    }
    

    上面的方法设置 contentOffset 滚动视图的值和 textFiled 向上移动,而 textField resignFirstResponder 调用下面的委托方法,您可以在其中设置 contentOffset

    - (void)textFieldDidEndEditing:(UITextField *)textField{
           scrollView.contentOffset = CGPointMake(0,-80);
    }
    

    注意:您需要使视图中的每个文本字段都将其委托作为 UIViewController 实例 . 您还需要 UIViewController 采用 UITextFieldDelegate

  • 0

    看看你的代码,在尝试定位滚动视图时,你不需要改变内容插入等 . 您只需要修改内容偏移属性 .

    这是修改后的代码:

    @interface ViewController () {
    UITextField *activeField;
    CGPoint scrollViewOldPosition;
    }
    

    修改keyboardWasShow如下:

    // Called when the UIKeyboardDidShowNotification is sent.
     - (void)keyboardWasShown:(NSNotification*)aNotification
    {
        NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
    
    CGFloat someSpaceBetweenKeyBoardAndField = 20.0;
    scrollViewOldPosition = self.scrollView.contentOffset;
    
    self.scrollView.contentOffset = CGPointMake(0, kbSize.height - (self.view.frame.size.height - activeField.frame.origin.y - activeField.frame.size.height) + someSpaceBetweenKeyBoardAndField);
    }
    

    键盘将隐藏方法:

    // Called when the UIKeyboardWillHideNotification is sent
    - (void)keyboardWillBeHidden:(NSNotification*)aNotification
    {
        self.scrollView.contentOffset = scrollViewOldPosition;
    }
    
  • 0

    不是最好的代码,但它有一些你可以使用的功能,任何带_的都是全局变量

    //Handle notification when keyboard appear
    - (void)keyboardOnScreen:(NSNotification *)notification
    {
        if (_isKeyboardShow) {
            return; //If keyboard is showing then return
        }
        _keyboardHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
        [self animateTextFieldUp: YES];
        _isKeyboardShow = YES; 
    }
    
    //Handle notification when keyboard hide
    - (void)keyboardOffScreen:(NSNotification *)notification
    {
        if(!_isKeyboardShow) return;
        [self animateTextFieldUp: NO];
        _isKeyboardShow = NO;//Missed this line
    }
    
    //Push view up with animation when keyboard show
    - (void) animateTextFieldUp: (BOOL) up
    {
        UITextField *textfield = [UIResponder currentFirstResponder];
        CGPoint windowPoint = [textfield convertPoint:textfield.bounds.origin toView:self.view];
        int movementDistance;
        CGPoint point = [_mainScrollView contentOffset];
    
        //Push up only when blocked by keyboard
        if (windowPoint.y + textfield.frame.size.height >= self.view.frame.size.height - _keyboardHeight) {
            movementDistance = windowPoint.y - (self.view.frame.size.height - _keyboardHeight) +  textfield.frame.size.height + 10;
            _oldMovementDistance = movementDistance;
            int movement = (up ? -movementDistance : movementDistance);
        [_mainScrollView setContentOffset:CGPointMake(0, point.y - movement) animated:YES];
        }
        else { //Push view down the same amount
            int movement = (up ? -movementDistance : _oldMovementDistance);
            [_mainScrollView setContentOffset:CGPointMake(0, point.y - movement) animated:YES];
            _oldMovementDistance = 0;
        }
    }
    
  • 0

    在编辑过程中将UITextField和UITextView移出键盘:

    对于非UITableViewControllers,将 TPKeyboardAvoidingScrollView.mTPKeyboardAvoidingScrollView.h 源文件放入项目中,将UIScrollView弹出到视图控制器's xib or storyboard, set the scroll view's类中以TPKeyboardAvoidingScrollView,并将所有控件放在该滚动视图中 . 您也可以在不使用xib的情况下以编程方式创建它 - 只需使用TPKeyboardAvoidingScrollView作为顶级视图 .

    要与UITableViewController类一起使用,请将TPKeyboardAvoidingTableView.m和TPKeyboardAvoidingTableView.h拖放到项目中,并使您的UITableView成为xib中的TPKeyboardAvoidingTableView . 如果您没有在控制器中使用xib,我知道没有简单的方法可以使其UITableView成为自定义类:阻力最小的路径是为它创建一个xib .

    你可以从here获得参考 .

    希望这可以帮助 .

相关问题