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

时间:2009-08-21 16:32:36

标签: iphone uitableview sdk

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

4 个答案:

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

答案 1 :(得分:1)

两个选项。 (当你说滚动视图我认为你的意思是表格视图,因为标题暗示如此)

  1. 使用UITableViewController,如果键盘已添加到表格视图中,我相信它会为您调整大小

  2. - (void)textFieldDidBeginEditing:(UITextField *)textField方法的文本字段或文本框协议中,调整表格视图框架的大小,结束完成编辑后将其设置回(如果它是滚动视图而不是的tableView)

    • (void)textFieldDidBeginEditing:(UITextField *)textField { CGRect frame = tableView.frame; //或滚动视图 frame.size.height = frame.size.height- keyboardHeight; tableView.frame =帧 }

    • (void)textFieldDidEndEditing:(UITextField *)textField { CGRect frame = tableView.frame; //或滚动视图 frame.size.height = frame.size.height + keyboardHeight; tableView.frame =帧 }

  3. 希望这有帮助

答案 2 :(得分:0)

使用Interface Builder或代码的另一种简单方法

将[表格视图大小页脚高度]属性设置为200(对于您的布局,请使用此属性)。这有效地使表格大于其所包含的边界框架,允许表格向上滚动高于正常。

非常适合让用户根据需要上下滚动表格。

答案 3 :(得分:0)

我确实创建了一个用键盘解决这个问题的小项目,在我的情况下我只需要在键盘出现时使表格视图上升。

希望这有帮助!

http://git.io/BrH9eQ