如何获得ios 8&amp ;;的键盘位置在numberPad上添加DONE按钮 [英] how to get keyboard location in ios 8 & add DONE button on numberPad

查看:162
本文介绍了如何获得ios 8&amp ;;的键盘位置在numberPad上添加DONE按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using below code to get the keyboard location from view & add DONE button on it.But in ios 8 it is not able to get keyboard location & hence not add DONE button.

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 上显示DONE按钮。
我使用 iOS 6/7/8设备在XCode-5.1.1中运行此代码。它的工作完美。

The below code is for showing "DONE" button on NumberPad iOS 8 also. I run this code in XCode-5.1.1 with iOS 6/7/8 devices. Its working perfectly.

我从这个链接中得到了参考找不到键盘iPhone-Portrait-NumberPad的类型4的键盘,给出了数字键盘上添加按钮的一些代码。

I take the refrence from this link Can't find keyplane that supports type 4 for keyboard iPhone-Portrait-NumberPad given some code for add button on Number keyboard.

@property (nonatomic, retain) UIButton *doneButton;

AddButton

- (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,真实设备 - 除非键盘显示动画已完成,否则不会添加该按钮。键盘完全可见后,下面的代码会添加延迟添加按钮:

Update: Testing on iOS 7.1, real device - the button is not being added unless the keyboard show animation has finished. The code below adds a delay to add button once the keyboard is fully visible:

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self performSelector:@selector(addButtonToKeyboard) withObject:nil afterDelay:0.75];

}

这篇关于如何获得ios 8&amp ;;的键盘位置在numberPad上添加DONE按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆