10

我已经搜索了很多代码来调整表格视图的大小以适应键盘显示和隐藏,但是我遇到的几乎每一篇文章都假设表格视图正在占据其视图控制器的整个视图。我有一个 iPad 应用程序,其中表格视图只占屏幕的一部分。在这种情况下调整表格视图大小的正确方法是什么?(我上面提到的帖子中的所有代码都失败了)

4

7 回答 7

17

以下代码可以满足您的需求,并适用于任何设备和任何布局。该代码由Sensible TableView 框架提供(具有复制和使用权限)。

- (void)keyboardWillShow:(NSNotification *)aNotification
{
if(keyboardShown) 
    return;

keyboardShown = YES;

// Get the keyboard size
UIScrollView *tableView;
if([self.tableView.superview isKindOfClass:[UIScrollView class]])
    tableView = (UIScrollView *)self.tableView.superview;
else
    tableView = self.tableView;
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [tableView.superview convertRect:[aValue CGRectValue] fromView:nil];

// Get the keyboard's animation details
NSTimeInterval animationDuration;
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
UIViewAnimationCurve animationCurve;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];

// Determine how much overlap exists between tableView and the keyboard
CGRect tableFrame = tableView.frame;
CGFloat tableLowerYCoord = tableFrame.origin.y + tableFrame.size.height;
keyboardOverlap = tableLowerYCoord - keyboardRect.origin.y;
if(self.inputAccessoryView && keyboardOverlap>0)
{
    CGFloat accessoryHeight = self.inputAccessoryView.frame.size.height;
    keyboardOverlap -= accessoryHeight;

    tableView.contentInset = UIEdgeInsetsMake(0, 0, accessoryHeight, 0);
    tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, accessoryHeight, 0);
}

if(keyboardOverlap < 0)
    keyboardOverlap = 0;

if(keyboardOverlap != 0)
{
    tableFrame.size.height -= keyboardOverlap;

    NSTimeInterval delay = 0;
    if(keyboardRect.size.height)
    {
        delay = (1 - keyboardOverlap/keyboardRect.size.height)*animationDuration;
        animationDuration = animationDuration * keyboardOverlap/keyboardRect.size.height;
    }

    [UIView animateWithDuration:animationDuration delay:delay 
                        options:UIViewAnimationOptionBeginFromCurrentState 
                     animations:^{ tableView.frame = tableFrame; } 
                     completion:^(BOOL finished){ [self tableAnimationEnded:nil finished:nil contextInfo:nil]; }];
}
}

- (void)keyboardWillHide:(NSNotification *)aNotification
{
if(!keyboardShown)
    return;

keyboardShown = NO;

UIScrollView *tableView;
if([self.tableView.superview isKindOfClass:[UIScrollView class]])
    tableView = (UIScrollView *)self.tableView.superview;
else
    tableView = self.tableView;
if(self.inputAccessoryView)
{
    tableView.contentInset = UIEdgeInsetsZero;
    tableView.scrollIndicatorInsets = UIEdgeInsetsZero;
}

if(keyboardOverlap == 0)
    return;

// Get the size & animation details of the keyboard
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [tableView.superview convertRect:[aValue CGRectValue] fromView:nil];

NSTimeInterval animationDuration;
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
UIViewAnimationCurve animationCurve;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];

CGRect tableFrame = tableView.frame; 
tableFrame.size.height += keyboardOverlap;

if(keyboardRect.size.height)
    animationDuration = animationDuration * keyboardOverlap/keyboardRect.size.height;

[UIView animateWithDuration:animationDuration delay:0 
                    options:UIViewAnimationOptionBeginFromCurrentState 
                 animations:^{ tableView.frame = tableFrame; } 
                 completion:nil];
}

- (void) tableAnimationEnded:(NSString*)animationID finished:(NSNumber *)finished contextInfo:(void *)context
{
// Scroll to the active cell
if(self.activeCellIndexPath)
{
    [self.tableView scrollToRowAtIndexPath:self.activeCellIndexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
    [self.tableView selectRowAtIndexPath:self.activeCellIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}

笔记:

一个。上面两个方法已经被添加到通知中心,使用如下代码:

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

湾。上面使用的 ivars 声明如下:

BOOL keyboardShown;
CGFloat keyboardOverlap;

C。'self.activeCellIndexPath' 始终设置为拥有当前活动 UITextField/UITextView 的单元格的 indexPath。

享受!:)

于 2012-12-13T19:36:02.270 回答
6

我发现最简单的解决方案就是这个(我不喜欢使用子视图来处理这种东西):

注册键盘框架更改通知(最好在 viewWillAppear 中注册:并在 viewWillDisappear 中取消注册:):

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];

然后在方法中:

- (void)keyboardDidChangeFrame:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    CGRect kbFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect kbIntersectFrame = [window convertRect:CGRectIntersection(window.frame, kbFrame) toView:self.scrollView];
    kbIntersectFrame = CGRectIntersection(self.bounds, kbIntersectFrame);

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbIntersectFrame.size.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
}

或者如果您想在更改 contentInset 后摆脱“跳转”:

- (void)keyboardDidChangeFrame:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    CGRect kbFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect kbIntersectFrame = [window convertRect:CGRectIntersection(window.frame, kbFrame) toView:self.scrollView];
    kbIntersectFrame = CGRectIntersection(self.scrollView.bounds, kbIntersectFrame);

    // get point before contentInset change
    CGPoint pointBefore = self.scrollView.contentOffset;
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbIntersectFrame.size.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
    // get point after contentInset change
    CGPoint pointAfter = self.scrollView.contentOffset;
    // avoid jump by settings contentOffset
    self.scrollView.contentOffset = pointBefore;
    // and now animate smoothly
    [self.scrollView setContentOffset:pointAfter animated:YES];
}
于 2013-10-29T14:06:22.300 回答
3

简单的解决方案是将我的扩展添加UIViewController+Keyboard.swift到您的项目中,只需一行setupKeyboardNotifcationListenerForScrollView(tableView),它将自动调整大小。不需要子类化任何东西,只是一个扩展!它在SingleLineKeyboardResize上的开源

于 2015-02-21T08:35:09.880 回答
2

简单的解决方案 - 注册以在 init 或 viewDidLoad 上接收键盘通知:

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

然后当收到通知时,您可以使用给定的键盘矩形来调整 tableView 的框架:

- (void)keyboardWillShow:(NSNotification *)notification
{
   // Get the size of the keyboard.
   CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

   CGRect newTableFrame = _myTableView.frame;
   //Here make adjustments to the tableview frame based on the value in keyboard size
   ...

   _myTableView.frame = newTableFrame;
}

- (void)keyboardWillHide:(NSNotification *)notification
{
   //Here change the table frame back to what it originally was.
}
于 2012-12-12T18:24:30.710 回答
1

看看这个项目,它是拖放的,所以只需将 tablview 声明为类型TPKeyboardAvoidingTableView

于 2012-12-12T18:16:27.100 回答
0

你可以通过使用IQKeyboardManager来实现你想要的,它是一个无代码库,你只需将它添加到你的 Podfile: pod 'IQKeyboardManager' 就是这样,当键盘显示时它会处理滚动效果,即使UITextField/UITextView 不是 scrollView/tableView 的一部分。

于 2016-08-16T19:50:33.670 回答
0

这是键盘方法:

func keyboardControl(notification: NSNotification, isShowing: Bool) {
    var userInfo = notification.userInfo!
    let keyboardRect = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue
    let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey]!.unsignedIntValue

    let convertedFrame = self.view.convertRect(keyboardRect, fromView: nil)
    let heightOffset = self.view.bounds.size.height - convertedFrame.origin.y
    let options = UIViewAnimationOptions(rawValue: UInt(curve) << 16)
    let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey]!.doubleValue

    //your UITableView bottom constrant
    self.tableViewMarginBottomConstraint.constant = heightOffset

    var contentInsets = UIEdgeInsetsZero
    if isShowing {
        contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardRect.size.height), 0.0)
    }

    UIView.animateWithDuration(
        duration,
        delay: 0,
        options: options.union(.LayoutSubviews).union(.BeginFromCurrentState),
        animations: {
            self.listTableView.contentInset = contentInsets
            self.listTableView.scrollIndicatorInsets = contentInsets
            self.listTableView.scrollBottomToLastRow()
            self.view.layoutIfNeeded()
        },
        completion: { bool in
    })
}

这是UITableView的扩展:</p>

extension UITableView {
    func totalRows() -> Int {
        var i = 0
        var rowCount = 0
        while i < self.numberOfSections {
            rowCount += self.numberOfRowsInSection(i)
            i++
        }
        return rowCount
    }

    var lastIndexPath: NSIndexPath {
        get { return NSIndexPath(forRow: self.totalRows()-1, inSection: 0) }
    }

    func scrollBottomToLastRow() {
        self.scrollToRowAtIndexPath(self.lastIndexPath, atScrollPosition: .Bottom, animated: false)
    }
}
于 2016-02-03T07:14:40.840 回答