【问题标题】:Set Tableview y-position in a UIViewController when Keyboard shows键盘显示时在 UIViewController 中设置 Tableview y 位置
【发布时间】:2015-01-04 13:17:27
【问题描述】:

我在 Main.Storyboard 中创建的 UIViewController 中有一个 TableView。 初始化时 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。

【问题讨论】:

标签: ios objective-c uitableview uiview uiviewcontroller


【解决方案1】:

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

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

你还应该设置:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 2011-08-05
    相关资源
    最近更新 更多