键盘显示时,在UIViewController中设置Tableview y位置

时间:2015-01-04 13:17:27

标签: ios objective-c uitableview uiview uiviewcontroller

我在Main.Storyboard中创建的TableView内有一个UIViewController。 初始化时Y位置为0。 我的问题是,当键盘显示时,TableView的Y位置根本没有变化。 Y位置保持为0.因此,单元格隐藏在NavigationController后面。

到目前为止的代码:

-(void) keyboardWasShown:(NSNotification*)aNotification
{
    if (kbIsShown == false) {
        NSDictionary* info = [aNotification userInfo];
        NSTimeInterval animationDuration;
        UIViewAnimationCurve animationCurve;
        CGRect keyboardFrame;
        [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
        [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
        [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame];

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:animationDuration];
        [UIView setAnimationCurve:animationCurve];
        NSLog(@"frame..%f..%f..%f..%f",self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
        NSLog(@"keyboard..%f..%f..%f..%f",keyboardFrame.origin.x, keyboardFrame.origin.y, keyboardFrame.size.width, keyboardFrame.size.height);
        [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - keyboardFrame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
        [chatTable setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + keyboardFrame.size.height + TABBAR_HEIGHT, self.view.frame.size.width, self.view.frame.size.height-keyboardFrame.size.height)];
        [UIView commitAnimations];
        NSLog(@"Visar sig");
        kbIsShown = true;
    }
}

[chatTable setFrame:]没有做任何事情。 TableViews (chatTable) Y位置保持为0.如何更改Tableview的Y位置以便我的内容可见?

注意:[self.view setFrame...]确实会改变视图的Y位置。但chatTable保持为0。

1 个答案:

答案 0 :(得分:0)

为了将表格视图滚动到所需的行使用:

[chatTable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];

您还应该设置:

chatTable.contentInset =  UIEdgeInsetsMake(0, 0, keyboardHeight, 0);