我的iPad上连接的设备充当外接键盘,但我的程序中仍然需要软键盘。我使用以下文章获得了一些运气:Show iphone soft keyboard even thought a hardware keyboard is connected,但iOS 5已经破坏了这种方法。在大多数视图中,软键盘的底部略微被切掉,但是其他键盘只有一半出现在窗口内,而不是你想象的那样。
以下所有代码都适用于iOS4中的AppDelegate.m文件,但不适用于iOS5。
在发送UITextFieldTextDidBeginEditingNotification后调用forceKeyboard。
-(void) textFieldBegan: (NSNotification *) theNotification
{
UITextField *theTextField = [theNotification object];
theTextField.inputAccessoryView = inputAccessoryView;
[self performSelector:@selector(forceKeyboard) withObject:nil afterDelay:0];
}
//Change the inputAccessoryView frame
-(void) forceKeyboard
{
inputAccessoryView.superview.frame = CGRectMake(0, 502, 1024, 265);
int movementDistance = 1;
float movementDuration = 0.3f;
[UIView beginAnimations: @"kb_anim_open" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
inputAccessoryView.superview.frame = CGRectOffset(inputAccessoryView.superview.frame, 0, movementDistance);
[UIView commitAnimations];
}
在发送UITextFieldTextDidEndEditingNotification后调用Close Keyboard。
-(void) textFieldEnd: (NSNotification *) theNotification
{
UITextView *theTextView = [theNotification object];
[theTextView resignFirstResponder];
[self performSelector:@selector(forceCloseKeyboard) withObject:nil afterDelay:0];
}
-(void) forceCloseKeyboard
{
inputAccessoryView.superview.frame = CGRectMake(0, 502, 1024, 265);
int movementDistance = 502;
float movementDuration = 0.3f;
[UIView beginAnimations: @"kb_anim_close" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
inputAccessoryView.superview.frame = CGRectOffset(inputAccessoryView.superview.frame, 0, movementDistance);
[UIView commitAnimations];
}
关于如何驯服软键盘的任何想法都将非常感激;我曾尝试过与superview.frame值摔跤一段时间,但没有运气。
感谢阅读。
更新
我已经找到了在iOS 4和5中打开和关闭键盘的设置,但没有动画。同样,这一切都在AppDelegate中,其中“textFieldBegan”和“textFieldEnd”由用户分别开始和结束编辑时发送的通知调用。
-(void) textFieldBegan: (NSNotification *) theNotification
{
UITextField *theTextField = [theNotification object];
if (!inputAccessoryView) {
inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 1)];
}else{
inputAccessoryView.frame = CGRectMake(0, 0, 1024, 1);
}
theTextField.inputAccessoryView = inputAccessoryView;
[self performSelector:@selector(forceKeyboard) withObject:nil afterDelay:0];
}
-(void) textFieldEnd: (NSNotification *) theNotification
{
NSString *classstr = NSStringFromClass([[theNotification object] class]);
UITextView *theTextView = [theNotification object];
[self performSelector:@selector(forceCloseKeyboard) withObject:nil afterDelay:0];
[theTextView resignFirstResponder];
}
//Change the inputAccessoryView frame
-(void) forceKeyboard
{
//default center = 512, 591
inputAccessoryView.superview.frame = CGRectMake(0, 415, 1024, 353);
[UIView animateWithDuration:0.3 animations: ^ { [inputAccessoryView.superview transform]; }];
}
-(void) forceCloseKeyboard //Unnecessary?
{
inputAccessoryView.frame = CGRectMake(0, 0, 1024, 0);
[UIView animateWithDuration:0.3 animations: ^ { [inputAccessoryView transform]; }];
}
看起来好像“forceCloseKeyboard”方法并没有真正做任何事情,因为当textField重新响应时它看起来像是关闭了键盘。我把它放在这里以防万一有人能想出一种方法来设置键盘的开启和关闭动画。