【问题标题】:how to get keyboard location in ios 8 & add DONE button on numberPad如何在 ios 8 中获取键盘位置并在 numberPad 上添加 DONE 按钮
【发布时间】:2014-09-25 06:11:42
【问题描述】:

我正在使用下面的代码从视图中获取键盘位置并在其上添加 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];
    }
}

【问题讨论】:

    标签: keyboard ios8


    【解决方案1】:

    以下代码也用于在 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;
    

    添加按钮

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

    【讨论】:

    • Gr8 成功了,但是当我将文本字段从小键盘更改为普通键盘时,完成按钮出现在键盘前面..我该怎么办?
    • 您必须在“textFieldShouldEndEditing”委托方法中调用 removeButtonFromKeyboard。就是这样。
    • 老兄当我直接点击文本字段完成按钮被隐藏,如果我再次从向下到向上点击它就会消失。怎么办?
    • @iAnurag - 请点击此链接stackoverflow.com/questions/25842168/…它将满足您的所有需求。
    • 我想从键盘上删除完成按钮和预测文本栏。我只想显示键盘请帮忙。
    【解决方案2】:
    • 我喜欢“Alex-stone”,但无法点击自定义按钮。

    • 我的解决方案:

    //创建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];
    }
    

    【讨论】:

      【解决方案3】:

      好的,当我遇到类似错误时,这是​​一个简单的修复方法,用于在 iOS 9、iOS 8 及更低版本的应用程序中显示和运行“完成”按钮。在运行应用程序并通过“视图的层次结构”查看它后可以观察到它(即,当应用程序在设备上运行时单击调试区域栏中的“查看层次结构”图标并在 Storyboard 中检查您的视图) ,与 iOS 8 及以下版本相比,键盘在 iOS 9 中显示在不同的窗口上,必须加以考虑。 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];
              }
          }
        }
      }
      

      如果您使用 UITextfieldsDelegates 方法,您只需在 textFieldDidBeginEditing 委托方法中调用 addButtonToKeyboard 方法即可。现在,如果您在 NumberPad 和默认键盘之间来回切换,建议您在“textFieldShouldEndEditing”委托方法中调用 removeKeyboardButton 以防止发生任何意外。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-19
        • 2014-02-13
        • 2011-08-01
        • 2021-07-20
        • 1970-01-01
        • 2014-01-09
        • 1970-01-01
        • 2013-12-10
        相关资源
        最近更新 更多