gpt4 book ai didi

keyboard - 如何在 ios 8 中获取键盘位置并在数字键盘上添加“完成”按钮

转载 作者:行者123 更新时间:2023-12-02 21:58:51 26 4
gpt4 key购买 nike

我使用下面的代码从 View 中获取键盘位置并在其上添加“完成”按钮。但在 ios 8 中,它无法获取键盘位置,因此无法添加“完成”按钮。

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];
}
}

最佳答案

下面的代码也用于在 NumberPad iOS 8 上显示 “完成” 按钮。我在 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;

添加按钮

- (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{}
}
}

从键盘中删除按钮

- (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];

}

关于keyboard - 如何在 ios 8 中获取键盘位置并在数字键盘上添加“完成”按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26031798/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com