简体   繁体   English

在textField成为第一响应者的同时获取外部/虚拟键盘的事件

[英]Get event for external/virtual keyboard while textField become first responder

In my iPad application, i am presenting a controller using form sheet style as 在我的iPad应用程序中,我正在展示一个使用表单样式的控制器

controller.modalPresentationStyle=UIModalPresentationFormSheet;

In landscape mode while device's keyboard open im setting size of tableView so that user can able to see all records of table. 在横向模式下,设备的键盘打开时会设置tableView的大小,以便用户可以查看表的所有记录。

To get event of show/hide keyboard. 获取显示/隐藏键盘事件。 I have set NSNotification 我已经设置了NSNotification

Problem 问题

But when user tap in textField of table cell using external/virtual keyboard, im not getting event of keyboard show/hide. 但是,当用户使用外部/虚拟键盘点击表格单元格的textField时,我没有收到键盘显示/隐藏事件。 So when textfield becomes first responder, Tableview size is decreasing but it's no need while user connected with external keyboard. 因此,当文本字段成为第一响应者时,Tableview的大小会减小,但是在用户使用外部键盘连接时并不需要。

Can anyone please guide/help here, what can i do? 任何人都可以在这里指导/帮助我该怎么办? So that i can stop do set size when using external keyboard. 这样我就可以在使用外接键盘时停止设置大小。

Register Keyboard Event 注册键盘事件

- (void)registerForKeyboardNotifications{

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasHidden:)
                                             name:UIKeyboardDidHideNotification object:nil];

}

Set Frame While AutoRotate and Text Field Become First Responder 在自动旋转和文本字段成为第一响应者的同时设置框架

-(void)setFramesOfTable
{

CGRect rct=tableView.frame;
if(appDel.isThisIPad && ([[UIApplication sharedApplication] statusBarOrientation]==UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation]==UIInterfaceOrientationLandscapeRight) && [selectedField isFirstResponder])
{
    rct.size.height=400.0;
}
else
{
    rct.size.height=576.0;
}
tableView.frame=rct;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{

selectedField = textField;
[self setFramesOfTable];
}

-(NSUInteger)supportedInterfaceOrientations
{
[self setFramesOfTable];
return UIInterfaceOrientationMaskAll;
}

Thanks. 谢谢。

Its not a good idea to change the frame of the table when the text field begins editing. 当文本字段开始编辑时,更改表格的框架不是一个好主意。 On the iPad, the user can have an external keyboard, the docked keyboard or the split keyboard. 在iPad上,用户可以具有外部键盘,扩展坞键盘或拆分键盘。

If the user has an external keyboard, you don't need to resize your window. 如果用户使用外接键盘,则无需调整窗口大小。 The onscreen keyboard does not appear when using an external keyboard so there is no reason to resize windows. 使用外接键盘时,屏幕键盘不会出现,因此没有理由调整窗口大小。

If the user is using the split keyboard, you don't really need to worry about resizing windows. 如果用户使用拆分键盘,则您实际上不必担心调整窗口大小。 if they split the keyboard, they could put the keyboard in the middle of the UI, making it impossible (or at very least impractical) to rearrange your UI so its not covered by at least a small portion of the split keyboard. 如果他们拆分了键盘,则可以将键盘放在UI的中间,从而无法(或至少不切实际)重新排列UI,以使其至少不被拆分键盘的一小部分所覆盖。 If the user splits the keyboard and covers up important UI components, they need to move the keyboard out of the way. 如果用户拆分键盘并掩盖了重要的UI组件,则需要将键盘移开。

The best way to resize your UI is in the keyboard will ChangeFrame/Hide methods 调整UI大小的最佳方法是在键盘上使用ChangeFrame / Hide方法

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

inside your handlers of these events, you can get the keyboard height, and adjust the UI accordingly 在这些事件的处理程序中,您可以获取键盘高度,并相应地调整UI

-(void)keyboardWillChangeFrame:(NSNotification*)notification
{
    NSDictionary* info = [notification userInfo];
    NSValue* kbFrame = info[UIKeyboardFrameEndUserInfoKey];
    NSTimeInterval animationDuration = [info[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect keyboardFrame = [kbFrame CGRectValue];
    BOOL isPortrait = UIDeviceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
    CGFloat height = isPortrait ? keyboardFrame.size.height : keyboardFrame.size.width;
}

this gets you the animationDuration and the height of the keyboard so that you can use a UIView animateWithDuration block to animate the frame change to your tableview so that it is not obscured by the keyboard. 这将为您提供animationDuration和键盘的高度,以便您可以使用UIView animateWithDuration块为表格更改创建动画,以便不会被键盘遮挡。

in keyboardWillHide: you only need to get the animationDuration (the same way as above) from the NSNotification (the height will obviously be 0). 在keyboardWillHide中:您只需要从NSNotification(高度显然为0)中获取animationDuration(与上述方法相同)。 Then use another UIView animateWithDuration block to animate your tableview resizing back to its original size 然后使用另一个UIView animateWithDuration块为您的tableview设置动画,使其大小恢复为原始大小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM