如何在ios 8&中获得键盘位置在数字键盘上添加DONE按钮

时间:2014-09-25 06:11:43

标签: keyboard ios8

我正在使用下面的代码从视图&获取键盘位置在它上面添加DONE按钮。但是在ios 8中它无法获得键盘位置和放大器。因此不要添加DONE按钮。

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

3 个答案:

答案 0 :(得分:13)

以下代码用于在NumberPad iOS 8 上显示“DONE”按钮。 我使用 iOS 6/7/8设备在XCode-5.1.1中运行此代码。它的工作完美。

我从这个链接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];

}

答案 1 :(得分:0)

  • 我确实喜欢&#34; Alex-stone&#34;但我无法点击自定义按钮。

  • 我的解决方案:

  

//创建DoneCustomReturnKeyButton以添加到键盘

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

答案 2 :(得分:0)

好的,这是一个简单的解决方案,可以帮助您完成工作。当我遇到类似的错误时,按钮在iOS 9,iOS 8及以下版本的应用程序中显示和工作。在运行应用程序并通过“查看的层次结构”查看后,可以观察到它。 (即,当应用程序在设备上运行并在Storyboard中检查您的视图时,单击“调试区域”栏中的“查看层次结构”图标),键盘将显示在iOS中的不同窗口上9与iOS 8及以下版本相比,必须加以考虑。 的 addButtonToKeyboard

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

如果你愿意,这就是你可以从键盘上 removeKeyboardButton 的方法。

- (void)removeKeyboardButton {

id windowTemp = [self addButtonToKeyboard];

if (windowTemp) {

    for (UIView *doneButton in [windowTemp subviews]) {
        if ([doneButton isKindOfClass:[UIButton class]]) {
            [doneButton setHidden:TRUE];
        }
    }
  }
}

如果您使用UITextfieldsDelegeates方法,则只需在textFieldDidBeginEditing委托方法中调用 addButtonToKeyboard 方法即可。现在,如果您在NumberPad和Default键盘之间来回切换,建议您在&#34; textFieldShouldEndEditing&#34;中调用 removeKeyboardButton 。委托方法以防止任何意外。