【问题标题】:IOS: UITextField+UITableView inside UIScrollViewIOS:UIScrollView里面的UITextField+UITableView
【发布时间】:2016-07-27 17:24:03
【问题描述】:

我是 iOS 新手。我正在制作一个聊天应用程序,所以我在UITableView 下方有一个UITextFieldUITextFieldUITableViewUIScrollView 内部(我已通过self.scrollView.scrollEnabled = NO; 禁用滚动视图中的滚动)。
当我点击UITextField 时,会显示键盘,所以我将UITextField 移动到键盘上方跟随Managing the Keyboard doc

   // Called when the UIKeyboardDidShowNotification is sent.
    - (void)keyboardWasShown:(NSNotification*)aNotification
    {
        NSDictionary* info = [aNotification userInfo];
        CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
        self.scrollView.contentInset = contentInsets;
        self.scrollView.scrollIndicatorInsets = contentInsets;

        // If active text field is hidden by keyboard, scroll it so it's visible
        // Your app might not need or want this behavior.
        CGRect aRect = self.view.frame;
        aRect.size.height -= kbSize.height;
        if (!CGRectContainsPoint(aRect, self.activeField.frame.origin) ) {
            [self.scrollView scrollRectToVisible:self.activeField.frame animated:YES];
        }
    }

    // Called when the UIKeyboardWillHideNotification is sent
    - (void)keyboardWillBeHidden:(NSNotification*)aNotification
    {
        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        self.scrollView.contentInset = contentInsets;
        self.scrollView.scrollIndicatorInsets = contentInsets;
    }

    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        self.activeField = textField;
    }

    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
        self.activeField = nil;
    }

使用此代码,我的TextField 成功移至键盘上方
但问题 是:在TableView 中,当显示键盘时,我无法滚动查看某些项目顶部(约 2 项)。
例如,如果我的 tableview 有 10 个项目,当显示键盘时,我只能看到 8 个项目,2 个其余项目仍然存在但我无法滚动到它们

这是我的Demo Project
我认为UITableView 的内容插入可能是错误的,但我不知道如何修复它。 任何帮助将不胜感激

【问题讨论】:

    标签: ios uitableview uiscrollview keyboard uitextfield


    【解决方案1】:

    使用Autolayout 代替contentInsets

    ViewController.h

    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *botConstraint;
    

    ViewController.m

    - (void)keyboardWasShown:(NSNotification*)aNotification
    {
        NSDictionary* info = [aNotification userInfo];
        CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
        self.botConstraint.constant = kbSize.height;
        [self.view layoutIfNeeded];
        //...
    }
    
    // Called when the UIKeyboardWillHideNotification is sent
    - (void)keyboardWillBeHidden:(NSNotification*)aNotification
    {
        self.botConstraint.constant = 0.0f;
        [self.view layoutIfNeeded];
        //...
    }
    

    botConstraintUITextField/UITableView/UIScrollView .bottom = fatherView.bottom 在您的Main.storyboard

    【讨论】:

    • 感谢您的回答。你能解释更多关于botConstraint的信息吗?当前,我为我的UITextField bottomUIScrollView bottom 添加了botConstraint。然后当显示键盘时,文本字段将出现在键盘顶部,我可以滚动我的TableView 中的所有项目。似乎解决了我的问题。但一个大问题是,在键盘显示期间,TextField 不随键盘滑动,TextField 仅在键盘显示后出现。当键盘显示时,tableview 不会为任何东西设置动画,我希望当键盘显示时 TableView 也滑动到顶部。请指导我我错过了什么
    • 更新你的问题代码,这样我就可以看到你到目前为止所做的尝试。
    • 我的帖子中包含了演示项目。如果你不介意,你可以检查一下。我只是改变,只改变我的代码,就像你的代码一样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 2016-11-11
    相关资源
    最近更新 更多