Ios 打开键盘时向上滚动tableView

Ios 打开键盘时向上滚动tableView,ios,swift,uitableview,cocoa-touch,Ios,Swift,Uitableview,Cocoa Touch,我有一个带有一些自定义单元格的UITable,在最后一个单元格上有一个UITextField,我希望所有单元格在打开键盘时都会向上滚动(所有类型的键盘,启用和禁用“预测”功能) 我已经研究了关于这个主题的其他问题,并尝试过,但仍然不够好(单元格与键盘之间存在间隙,tableView和键盘的动画没有同步,等等) 你能帮我吗 谢谢大家! 请查看上的iOS Firechat示例项目。它在tableView之外使用一个文本字段,并在视图出现/消失时将其与键盘一起移动 具体而言,本节: // Setup

我有一个带有一些自定义单元格的UITable,在最后一个单元格上有一个
UITextField
,我希望所有单元格在打开键盘时都会向上滚动(所有类型的键盘,启用和禁用“预测”功能)

我已经研究了关于这个主题的其他问题,并尝试过,但仍然不够好(单元格与键盘之间存在间隙,tableView和键盘的动画没有同步,等等)

你能帮我吗


谢谢大家!

请查看上的iOS Firechat示例项目。它在tableView之外使用一个文本字段,并在视图出现/消失时将其与键盘一起移动

具体而言,本节:

// Setup keyboard handlers to slide the view containing the table view and
// text field upwards when the keyboard shows, and downwards when it hides.
- (void)keyboardWillShow:(NSNotification*)notification
{
    [self moveView:[notification userInfo] up:YES];
}

- (void)keyboardWillHide:(NSNotification*)notification
{
    [self moveView:[notification userInfo] up:NO];
}

- (void)moveView:(NSDictionary*)userInfo up:(BOOL)up
{
    CGRect keyboardEndFrame;
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]
     getValue:&keyboardEndFrame];

    UIViewAnimationCurve animationCurve;
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey]
     getValue:&animationCurve];

    NSTimeInterval animationDuration;
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]
     getValue:&animationDuration];

    // Get the correct keyboard size to we slide the right amount.
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:animationCurve];

    CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
    int y = keyboardFrame.size.height * (up ? -1 : 1);
    self.view.frame = CGRectOffset(self.view.frame, 0, y);

    [UIView commitAnimations];
}
在Swift中也是如此,未经测试:

func moveView(userInfo : NSDictionary, up : Bool){

        var keyboardEndFrame : CGRect?
        userInfo.objectForKey(UIKeyboardFrameEndUserInfoKey)?.getValue(&keyboardEndFrame)

        var animationCurve : UIViewAnimationCurve?
        userInfo.objectForKey(UIKeyboardAnimationCurveUserInfoKey)?.getValue(&animationCurve)

        var animationDuration : NSTimeInterval?
        userInfo.objectForKey(UIKeyboardAnimationDurationUserInfoKey)?.getValue(&animationDuration)

        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(animationDuration!)
        UIView.setAnimationCurve(animationCurve!)

        let keyboardFrame = self.view.convertRect(keyboardEndFrame!, toView: nil)

        let y = keyboardFrame.size.height * (up ? -1 : 1);
        self.view.frame = CGRectOffset(self.view.frame, 0, y);

        UIView.commitAnimations()
    }

检查此项。==>理论上,如果您需要在swift中使用,只需更改
tableview
FS.O5的
scrollView
,因此请删除问题中的objective-c标记。