ios - 如何在 UIKeyboard 可见时更改 UITableView 大小?

标签 ios objective-c uitableview uitextfield uikeyboard

我有一个带有 UITableView 的应用程序,它在 UITableViewCells 中有 UITextFields,用户将连接虚拟 UIKeyboard 或蓝牙键盘以在 UITextFields 中输入文本。

如果连接了蓝牙键盘,我想在选择和输入文本时保持 UITableView 的全高。当没有蓝牙键盘连接时,如果它可见,我想缩小 UITableview 以适应 UIkeyboard 的显示,如果它没有显示,那么我想再次使 UITableView 成为全尺寸。

我尝试使用 UIKeyboard 委托(delegate)方法来做到这一点

- (void)keyboardDidShow:(NSNotification *)aNotification;
- (void)keyboardDidHide:(NSNotification *)aNotification;

出于某种原因,keyboardDidShow 从未被访问过,但是当 UIKeyboard 从 View 中移除时,keyboardDidHide 被访问了,我不知道为什么。

最佳答案

- (void)keyboardDidShow:(NSNotification *)aNotification;
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3f];
    self.tblView.frame = CGRectMake(0, height, self.view.frame.size.width, self.view.frame.size.height - 255); // or -216
    [UIView commitAnimations];
}

- (void)keyboardDidHide:(NSNotification *)aNotification;
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.2f];
    self.tblView.frame = CGRectMake(0, height, self.view.frame.size.width, self.view.frame.size.height - height);
    [UIView commitAnimations];
}

并且在 UITextField 委托(delegate)方法中

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{   
    CGRect rc = [textField bounds];
    rc = [textField convertRect:rc toView:self.tblView];
    CGPoint pt = rc.origin;
    pt.x = 0;
    if(rc.origin.y > 200)
        pt.y -=  150;
    else
        pt.y -= rc.origin.y;
    [self.tblView setContentOffset:pt animated:YES];

    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

关于ios - 如何在 UIKeyboard 可见时更改 UITableView 大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21643029/

相关文章:

ios - 在 tableViewController 中接收 Firebase autoId

iphone - 如何制作带列的 UITable?

iphone - 计算 NSMutableArray 中有多少个唯一行

ios - 在 iOS 设备中通过 wifi 或无线接入点进行通信

ios - Swift:如何在实例化 viewController 之前加载 Firebase 数据?

ios - UISearchBar 在点击时滑开

swift - 尝试在 TableView 中快速查找特定单元格时失败

ios - Estimote iOS SDK Examples 找不到要选择的信标

ios - 使用跟随手指 ios 显示两个隐藏 View

ios - 将变量添加到 block