通用UITableView键盘调整大小算法

时间:2012-12-12 17:36:37

标签: iphone ios ipad uitableview

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

7 个答案:

答案 0 :(得分:17)

以下代码可以满足您的需求,适用于任何设备和任何布局。该代码由Sensible TableView framework提供(允许复制和使用)。

- (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;

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

享受! :)

答案 1 :(得分: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];
}

答案 2 :(得分:3)

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

答案 3 :(得分: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.
}

答案 4 :(得分:1)

Check out this project,它是拖放,所以只需将tablview声明为TPKeyboardAvoidingTableView

类型

答案 5 :(得分: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扩展:

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)
    }
}

答案 6 :(得分:0)

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