以编程方式更改UITableView的滚动插入以反映iPhone键盘的大小

jos*_*lat 2 iphone sdk uitableview

我知道您可以使用Interface Builder中的"Inset"属性使滚动视图从主窗口插入,以便它不会低于屏幕上的现有控件(如标签栏),但是如何以编程方式执行此操作调整键盘何时添加到屏幕?目前我的滚动视图在键盘下方有单元格无法访问,因为视图仍然将滚动视图的底部注册为手机底部,而不是键盘顶部.有帮助吗?

slf*_*slf 7

#pragma mark Keyboard Handling



- (void) viewDidAppear:(BOOL) ani {
    onscreen = YES;

    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
    [center addObserver:self selector:@selector(keyboardDidAppear:) name:UIKeyboardDidShowNotification object:nil];
    [center addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];
}

- (void) viewDidDisappear:(BOOL) ani {
    onscreen = NO;

    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
    [center removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [center removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [center removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void) keyboardWillAppear:(NSNotification*) n {
    NSLog(@"Keyboard is about to appear");
}

- (void) keyboardDidAppear:(NSNotification*) n {

    CGRect bounds = [[[n userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
    bounds = [self.view convertRect:bounds fromView:nil];

    CGRect tableFrame = searchResultsTable.frame;
    tableFrame.size.height -= bounds.size.height; // subtract the keyboard height
    if (self.tabBarController != nil) {
        tableFrame.size.height += 48; // add the tab bar height
    }

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(shrinkDidEnd:finished:contextInfo:)];
    searchResultsTable.frame = tableFrame;
    [UIView commitAnimations];

    //[self hideEditorView:currentEditorView];
    //[currentEditorView removeFromSuperview];
}

- (void) shrinkDidEnd:(NSString*) ident finished:(BOOL) finished contextInfo:(void*) nothing {
    NSIndexPath* sel = [searchResultsTable indexPathForSelectedRow];

    if (![[searchResultsTable indexPathsForVisibleRows] containsObject:sel])
    {
        [searchResultsTable scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }
}

- (void) keyboardWillDisappear:(NSNotification*) n {
    CGRect bounds = [[[n userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
    bounds = [self.view convertRect:bounds fromView:nil];

    CGRect tableFrame = searchResultsTable.frame;
    tableFrame.size.height += bounds.size.height; // add the keyboard height

    if (self.tabBarController != nil) {
        tableFrame.size.height -= 48; // subtract the tab bar height
    }

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(shrinkDidEnd:finished:contextInfo:)];
    searchResultsTable.frame = tableFrame;    
    [UIView commitAnimations];

    [searchResultsTable scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
Run Code Online (Sandbox Code Playgroud)