自定义键盘 (iPhone)、UIKeyboardDidShowNotification 和 UITableViewController

发布于 2024-08-25 02:57:28 字数 1514 浏览 6 评论 0原文

在 iPhone 应用程序上,我有一个自定义键盘,其工作方式与标准键盘类似;如果自定义文本字段成为第一响应者,它就会出现;如果该字段放弃第一响应者,它就会隐藏。我还发布了通用 UIKeyboardWillShowNotificationUIKeyboardDidShowNotification 及其隐藏对应项,如下所示:

NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithCapacity:5];
[userInfo setObject:[NSValue valueWithCGPoint:self.center]
             forKey:UIKeyboardCenterBeginUserInfoKey];
[userInfo setObject:[NSValue valueWithCGPoint:shownCenter]
             forKey:UIKeyboardCenterEndUserInfoKey];
[userInfo setObject:[NSValue valueWithCGRect:self.bounds]
             forKey:UIKeyboardBoundsUserInfoKey];
[userInfo setObject:[NSNumber numberWithInt:UIViewAnimationCurveEaseOut]
             forKey:UIKeyboardAnimationCurveUserInfoKey];
[userInfo setObject:[NSNumber numberWithDouble:thisAnimDuration]
             forKey:UIKeyboardAnimationDurationUserInfoKey];

[[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardWillShowNotification
                                                    object:nil
                                                  userInfo:userInfo];

此代码正在运行,我在 UIViewController 子类中使用它。

现在,从 iPhone OS 3.0 开始,当系统键盘显示和隐藏时,UITableViewController 会自动调整其 tableView 的大小。我现在才针对 3.0 进行编译,我认为如果出现我的自定义键盘,控制器也应该调整表格大小,因为我发布了相同的通知。然而事实并非如此。表视图控制器被设置为输入字段的委托。

有谁知道为什么会出现这种情况?有人成功实施过类似的事情吗?

我有标准输入字段和自定义输入字段,因此,如果用户更改字段,标准键盘会隐藏,而自定义键盘会显示。如果 tableView 没有调整到完整高度并且我不必使用自定义方法将其调整回来,那将是有益的。

On an iPhone App, I've got a custom keyboard which works like the standard keyboard; it appears if a custom textfield becomes first responder and hides if the field resigns first responder. I'm also posting the Generic UIKeyboardWillShowNotification, UIKeyboardDidShowNotification and their hiding counterparts, like follows:

NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithCapacity:5];
[userInfo setObject:[NSValue valueWithCGPoint:self.center]
             forKey:UIKeyboardCenterBeginUserInfoKey];
[userInfo setObject:[NSValue valueWithCGPoint:shownCenter]
             forKey:UIKeyboardCenterEndUserInfoKey];
[userInfo setObject:[NSValue valueWithCGRect:self.bounds]
             forKey:UIKeyboardBoundsUserInfoKey];
[userInfo setObject:[NSNumber numberWithInt:UIViewAnimationCurveEaseOut]
             forKey:UIKeyboardAnimationCurveUserInfoKey];
[userInfo setObject:[NSNumber numberWithDouble:thisAnimDuration]
             forKey:UIKeyboardAnimationDurationUserInfoKey];

[[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardWillShowNotification
                                                    object:nil
                                                  userInfo:userInfo];

This code is working and I use it in UIViewController subclasses.

Now since iPhone OS 3.0, UITableViewController automatically resizes its tableView when the system keyboards show and hide. I'm only now compiling against 3.0 and I thought that the controller should also resize the table if my custom keyboard appears, since I'm posting the same notification. However it doesn't. The table view controller is set as the delegate of the input fields.

Does anyone have an idea why this might be the case? Has anyone implemented something similar successfully?

I have standard input fields along the custom ones, so if the user changes the fields the standard keyboard hides and the custom one shows. It would be beneficial if the tableView didn't resize to full height and I didn't have to resize it back with a custom method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捎一片雪花 2024-09-01 02:57:28

好吧,您有几种可能性。从您的描述来看,UITableView 似乎没有使用 UIKeyboard 通知。

但可能是 UINavigationController 响应此通知(或 UITabBarController)。

您可以在 tableview 中重写 setFrame:drawRect: 和 setNeedsDisplay 等方法,以查看调用堆栈中发生的情况。您也许能够弄清楚到底是什么导致表格视图以正确的大小重绘。

但很可能,自己更改 tableView 的大小是更简单的解决方案。这些建议只是为了好玩!

Well there are a few possibilities that you poke around in. From your description, it seems that UITableView is not using the UIKeyboard Notifications.

But possibly, it is the UINavigationController that is responding to this notification (or UITabBarController).

You could override methods like setFrame: drawRect: and setNeedsDisplay in the tableview to see what is happening in the call stack. You may be able to figure out what is actually causing the tableview to redraw at the correct size.

But in all likelihood, just changing the size of the tableView yourself is the much easier solution. These suggestions are just for fun!

深府石板幽径 2024-09-01 02:57:28

我也做过类似的事情。如果我记得的话,我最终只是让 TableViewController 服从您发送或系统发送的通知,然后将更改动画化到表视图的框架。据推测,内部正在做类似的事情,但我认为最终结果只是变成两个相互缠绕的动画块,它们在系统发布通知时运行,但最终结果应该是相同的。

在你的 viewdidLoad:

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(keyboardWillShow:) 
                                             name: UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(keyboardDidShowOrHide:) 
                                             name: UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(keyboardWillHide:) 
                                             name: UIKeyboardWillHidewNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(keyboardDidShowOrHide:) 
                                             name: UIKeyboardDidHideNotification object:nil];

和视图控制器中:

-(void) keyboardWillShow:(id)sender {
  [UIView beginAnimations];
  [UIView setAnimationDuration:0.3];
  self.view.frame = //Your new size
}

-(void) keyboardDidShowOrHide:(id)sender {
  [UIView commitAnimations];
}

-(void) keyboardWillHide:(id)sender {
  [UIView beginAnimations];
  [UIView setAnimationDuration:0.3];
  self.view.frame = //Your old size
}

I have done something similar. If I recall, I ended up just having the TableViewController subecibe to the notifications that either you send or the system sends, and then animate the change to the tableview's frame. Presumably there are doing something similar internally, but I think the end result just becomes two animation blocks wrapped around each other that both run when the system is posting the notifications, but the end result should be the same.

In your viewdidLoad:

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(keyboardWillShow:) 
                                             name: UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(keyboardDidShowOrHide:) 
                                             name: UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(keyboardWillHide:) 
                                             name: UIKeyboardWillHidewNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(keyboardDidShowOrHide:) 
                                             name: UIKeyboardDidHideNotification object:nil];

and in the view controller:

-(void) keyboardWillShow:(id)sender {
  [UIView beginAnimations];
  [UIView setAnimationDuration:0.3];
  self.view.frame = //Your new size
}

-(void) keyboardDidShowOrHide:(id)sender {
  [UIView commitAnimations];
}

-(void) keyboardWillHide:(id)sender {
  [UIView beginAnimations];
  [UIView setAnimationDuration:0.3];
  self.view.frame = //Your old size
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文