如何在我的工具栏上方移动文本字段,该工具栏添加到键盘上方

时间:2014-11-18 14:00:05

标签: ios

我添加了一个带按钮的工具栏,用于关闭键盘上方的键盘,并使用通知移动文本字段但是如何在scrollingRect时考虑工具栏的高度

// 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);
    scroll.contentInset = contentInsets;
    scroll.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, activeField.frame.origin) ) {
        [self.scroll scrollRectToVisible:activeField.frame animated:YES];
    }
}

1 个答案:

答案 0 :(得分:0)

为键盘附件视图的高度创建一个实例变量(例如float accessoryViewHeight),并在创建附件视图时进行设置。然后,在您发布的代码中,使用kbSize + the accessoryViewHeight代替kbSize.height.

示例:

...
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
float totalKBHeight = kbSize.height + accessoryViewHeight;

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, totalKBHeight, 0.0);
...
aRect.size.height -= totalKBHeight;