在下面的代碼是用於示出在數字小iOS 8的也「DONE」按鈕。 我在XCode-5.1.1中運行此代碼,使用iOS 6/7/8設備。它的工作完美。
我從這個鏈接Can't find keyplane that supports type 4 for keyboard iPhone-Portrait-NumberPad的參考資料給出了一些編號鍵盤上的添加按鈕的代碼。
@property (nonatomic, retain) UIButton *doneButton;
Add按鈕
- (void)addButtonToKeyboard
{
if (!self.doneButton)
{
self.doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.doneButton addTarget:self action:@selector(doneButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
self.doneButton.adjustsImageWhenHighlighted = NO;
[self.doneButton setTitle:@"DONE" forState:UIControlStateNormal];
[self.doneButton.titleLabel setFont:[UIFont systemFontOfSize:16.0]];
[self.doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
// locate keyboard view
if ([[[UIApplication sharedApplication] windows] count] <= 1) return;
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if ([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
{
BOOL isPortrait = UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
self.doneButton.frame = CGRectMake(((isPortrait)?0:-1),((int) (keyboard.frame.size.height*3)/4) + ((isPortrait)?0:1),(int) keyboard.frame.size.width/3-1, (isPortrait)?60:40);
[keyboard addSubview:self.doneButton];
}
//This code will work on iOS 8.0
else if([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] == YES)
{
for(int i = 0 ; i < [keyboard.subviews count] ; i++)
{
UIView* hostkeyboard = [keyboard.subviews objectAtIndex:i];
if([[hostkeyboard description] hasPrefix:@"<UIInputSetHost"] == YES)
{
BOOL isPortrait = UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
self.doneButton.frame = CGRectMake(((isPortrait) ? 0 : -1),((int) (hostkeyboard.frame.size.height*3)/4) + ((isPortrait) ? 0 : 1),(int) hostkeyboard.frame.size.width/3-1, (isPortrait) ? 60 : 40);
[hostkeyboard addSubview:self.doneButton];
}
}
}
else{}
}
}
removeButtonFromKeyboard
- (void)removeButtonFromKeyboard
{
NSArray *arTemp = [[UIApplication sharedApplication] windows];
if ([arTemp count] <= 1) return;
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if ([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
{
for (id temp in keyboard.subviews)
{
if ([temp isKindOfClass:[UIButton class]])
{
UIButton *btnDone = (UIButton*) temp;
[btnDone removeFromSuperview];
break;
}
}
}
//This code will work on iOS 8.0
else if([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] == YES)
{
for(int i = 0 ; i < [keyboard.subviews count] ; i++)
{
UIView* hostkeyboard = [keyboard.subviews objectAtIndex:i];
if([[hostkeyboard description] hasPrefix:@"<UIInputSetHost"] == YES)
{
for (id temp in hostkeyboard.subviews)
{
if ([temp isKindOfClass:[UIButton class]])
{
UIButton *btnDone = (UIButton*) temp;
[btnDone removeFromSuperview];
break;
}
}
}
}
}
else{}
}
}
讓我知道的任何問題。
更新: 在iOS 7.1,真實設備上測試 - 除非鍵盤顯示動畫已完成,否則不會添加按鈕。下面的代碼添加延遲添加按鈕一次鍵盤是完全可見:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[self performSelector:@selector(addButtonToKeyboard) withObject:nil afterDelay:0.75];
}
感謝ü它爲我工作 – poojathorat 2014-09-26 05:53:23
GR8它的工作,但是當我改變文本框從數字鍵盤普通鍵盤完成按鈕出現在keyboard..what面前我應該怎麼辦? – iAnurag 2014-09-26 13:35:45
你要叫removeButtonFromKeyboard在「textFieldShouldEndEditing」委託方法。而已。 – alishaik786 2014-09-26 15:07:30