我有一个带有 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/