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

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

问题描述

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

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.

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

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;

添加按钮

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

如有任何问题,请告诉我.

Let me know any issues.

更新:在 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天全站免登陆