Objective c 当键盘显示时,如何在选项卡视图中滚动?

Objective c 当键盘显示时,如何在选项卡视图中滚动?,objective-c,uiscrollview,tableview,Objective C,Uiscrollview,Tableview,我有一个滚动视图,其中有表格视图。我设置当键盘出现时,键盘上方的内容应该移动,这样键盘就不会遮挡任何东西。仅当键盘向上时,如何滚动键盘上方的内容?当键盘向上和向下时,您需要管理UITableView的高度。通知将有助于管理键盘是否打开的状态。根据状态,我们可以预测UITableView的高度 -(void) keyboardWillShow:(NSNotification *)note{ // get keyboard size and loctaion CGRect keyb

我有一个
滚动视图
,其中有
表格视图
。我设置当键盘出现时,键盘上方的内容应该移动,这样键盘就不会遮挡任何东西。仅当键盘向上时,如何滚动键盘上方的内容?

当键盘向上和向下时,您需要管理UITableView的高度。通知将有助于管理键盘是否打开的状态。根据状态,我们可以预测UITableView的高度

 -(void) keyboardWillShow:(NSNotification *)note{
    // get keyboard size and loctaion
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    // Need to translate the bounds to account for rotation.
    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
    // animations settings
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];
    // set views with new info
    _containerBottom.constant = keyboardBounds.size.height;
    CGRect rectTable = _tblMessages.frame;
    rectTable.size.height -= keyboardBounds.size.height;
    _tblMessages.frame = rectTable;
    [self.view layoutIfNeeded];
    [self scrollToBottom];

    // commit animations
    [UIView commitAnimations];
}

-(void) keyboardWillHide:(NSNotification *)note{

    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];

    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    // animations settings
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];


    CGRect rectTable = _tblMessages.frame;
    rectTable.size.height += keyboardBounds.size.height;
    _tblMessages.frame = rectTable;
    // commit animations
    [UIView commitAnimations];
}
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        // register for keyboard notifications
            [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];

    }

    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
        // unregister for keyboard notifications while not visible.
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

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

您可以添加观察者来检查键盘是否显示

 NotificationCenter.default.addObserver(self,
    selector: #selector(viewController.keyboardWillShow(notification:)),
    name: NSNotification.Name.UIKeyboardWillShow, object: nil)

NotificationCenter.default.addObserver(self,
    selector: #selector(viewController.keyboardWillHide(notification:)),
    name: NSNotification.Name.UIKeyboardWillHide, object: nil)

func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo? [UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
    print("Show") 
  }
}

 func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
    print("Hide")
  }
}
然后,您需要对内容进行计算,以显示键盘何时出现,并设置scrollview的内容偏移量