Keyboard 如何在ios 8中获取键盘位置&;在numberPad上添加完成按钮
我正在使用下面的代码从“查看并添加完成”按钮中获取键盘位置。但在ios 8中,它无法获取键盘位置,因此无法添加完成按钮Keyboard 如何在ios 8中获取键盘位置&;在numberPad上添加完成按钮,keyboard,ios8,Keyboard,Ios8,我正在使用下面的代码从“查看并添加完成”按钮中获取键盘位置。但在ios 8中,它无法获取键盘位置,因此无法添加完成按钮 UIWindow *tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; UIView *keyboard; for (int i = 0; i < [tempWindow.subviews count]; i++) { keyboard = [tempWindo
UIWindow *tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView *keyboard;
for (int i = 0; i < [tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
if ([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES)
{
[keyboard addSubview:doneButton];
}
}
UIWindow*tempWindow=[[UIApplication sharedApplication]windows]对象索引:1];
UIView*键盘;
对于(int i=0;i<[tempWindow.subviews计数];i++)
{
键盘=[tempWindow.subviews objectAtIndex:i];
//找到键盘视图;向其中添加自定义按钮
如果([[keyboard description]hasPrefix:@“以下代码也用于显示NumberPadiOS 8上的“完成”按钮。
我在XCode-5.1.1中用iOS 6/7/8设备运行这段代码。它工作得非常好
我引用了这个链接,给出了一些数字键盘上添加按钮的代码
@property (nonatomic, retain) UIButton *doneButton;
添加按钮
- (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{}
}
}
- 我确实喜欢“亚历克斯·斯通”,但我不能点击自定义按钮
- 我的解决方案:
//创建DoneCustomReturnKeyButton以添加到键盘中
好的,这里有一个简单的修复程序,当我遇到类似错误时,可以在iOS 9、iOS 8及以下版本的应用程序中显示并使用“完成”按钮。在运行应用程序并通过“查看层次结构”查看后,可以观察到该错误(即,当应用程序在设备上运行并在情节串连板中检查您的视图时,单击调试区域栏中的“视图层次结构”图标),与iOS 8及以下版本相比,iOS 9中的键盘显示在不同的窗口上,并且必须加以说明。
addButtonToKeyboard
- (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{}
}
}
- (id)addButtonToKeyboard
{
if (!doneButton)
{
// create custom button
UIButton * doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(-2, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:@selector(saveNewLead:) forControlEvents:UIControlEventTouchUpInside];
}
NSArray *windows = [[UIApplication sharedApplication] windows];
//Check to see if running below iOS 9,then return the second window which bears the keyboard
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) {
return windows[windows.count - 2];
}
else {
UIWindow* keyboardWithDoneButtonWindow = [ windows lastObject];
return keyboardWithDoneButtonWindow;
}
}
如果您使用UITextFieldsDelegates方法,只需在TextFielddBeginediting委托方法中调用addButtonToKeyboard方法即可。现在,如果您在NumberPad和默认键盘之间来回切换,建议您在“TextFieldsShoulDediting”中调用removeKeyboardButton“委派方法以防止任何意外事故。Gr8它起作用了,但当我将textfield从numpad更改为普通键盘时,键盘前面会出现“完成”按钮。我该怎么办?您必须在“textFieldShouldEndEditing”中调用键盘上的RemoveButton”委托方法。就是这样。伙计,当我直接点击文本字段“完成”按钮时,按钮会被隐藏,如果我再次从下向上点击,按钮会消失。怎么办?@iAnurag-请点击此链接,它会满足您的所有需要。我想删除“完成”按钮以及键盘上的预测文本栏。我只想显示按键请帮忙。
- (void)addDoneCustomReturnKeyButtonInKeyboard:(NSNotification *)notification
{
NSArray *arr = [[[[UIApplication sharedApplication] windows] lastObject] subviews];
if (arr.count > 0) {
UIView *keyboardView = [arr objectAtIndex:0];
float height;
if ([[keyboardView description] hasPrefix:@"<UIPeripheralHost"] == YES) {
height = (CGRectGetHeight(keyboardView.frame) -
CGRectGetHeight(self.navigationController.navigationBar.frame)) / 4;
[self.doneCustomReturnKeyButton setFrame:CGRectMake(0, keyboardView.frame.size.height - height,
keyboardView.frame.size.width/3 - 2, height)];
[keyboardView addSubview:self.doneCustomReturnKeyButton];
}
//This code will work on iOS 8.0
else if([[keyboardView description] hasPrefix:@"<UIInputSetContainerView"] == YES)
{
for(int i = 0 ; i < [keyboardView.subviews count] ; i++)
{
UIView* hostkeyboard = [keyboardView.subviews objectAtIndex:i];
if([[hostkeyboard description] hasPrefix:@"<UIInputSetHost"] == YES)
{
UIButton* donebtn = (UIButton*)[hostkeyboard viewWithTag:67123];
if (donebtn == nil)
{
height = (CGRectGetHeight(hostkeyboard.frame) -
CGRectGetHeight(self.navigationController.navigationBar.frame)) / 4;
[self.doneCustomReturnKeyButton setFrame:CGRectMake(0, keyboardView.frame.size.height - height,
keyboardView.frame.size.width/3 - 2, height)];
[keyboardView addSubview:self.doneCustomReturnKeyButton];
}
}
}
}
}
else {
self.doneCustomReturnKeyButton.hidden = YES;
}
- (void)createDoneCustomReturnKeyButton
{
self.doneCustomReturnKeyButton = [UIButton buttonWithType:UIButtonTypeSystem];
[self.doneCustomReturnKeyButton setTitle:NSLocalizedString(@"NEXT", nil) forState:UIControlStateNormal];
[self.doneCustomReturnKeyButton.titleLabel setFont:[UIFont systemFontOfSize:20]];
self.doneCustomReturnKeyButton.adjustsImageWhenHighlighted = NO;
self.doneCustomReturnKeyButton.backgroundColor = [UIColor lightGrayColor];
[self.doneCustomReturnKeyButton setTintColor:[UIColor whiteColor]];
[self.doneCustomReturnKeyButton addTarget:self
action:@selector(doneCustomReturnKeyButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
}
- (id)addButtonToKeyboard
{
if (!doneButton)
{
// create custom button
UIButton * doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(-2, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:@selector(saveNewLead:) forControlEvents:UIControlEventTouchUpInside];
}
NSArray *windows = [[UIApplication sharedApplication] windows];
//Check to see if running below iOS 9,then return the second window which bears the keyboard
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) {
return windows[windows.count - 2];
}
else {
UIWindow* keyboardWithDoneButtonWindow = [ windows lastObject];
return keyboardWithDoneButtonWindow;
}
}
- (void)removeKeyboardButton {
id windowTemp = [self addButtonToKeyboard];
if (windowTemp) {
for (UIView *doneButton in [windowTemp subviews]) {
if ([doneButton isKindOfClass:[UIButton class]]) {
[doneButton setHidden:TRUE];
}
}
}
}