没有完成按钮的UIKeyboardTypeNumberPad

Abh*_*nav 9 iphone cocoa-touch uikit uikeyboard ios

我们如何实现UIKeyboardTypeNumberPad以便它有一个"完成"按钮?默认情况下它没有.

Rah*_*rma 13

如果我没有错,那么您想询问如何为UIKeyboardTypeNumberPad的键盘添加自定义"完成"按钮.在这种情况下,这可能会有所帮助.在in.h中声明一个UIButton*doneButton并将以下代码添加到.m文件中

- (void)addButtonToKeyboard {
    // create custom button
    if (doneButton == nil) {
        doneButton  = [[UIButton alloc] initWithFrame:CGRectMake(0, 163, 106, 53)];
    }
    else {
        [doneButton setHidden:NO];
    }

    [doneButton addTarget:self action:@selector(doneButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    // locate keyboard view
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard = nil;
    for(int i=0; i<[tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard found, add the button
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
            if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
                [keyboard addSubview:doneButton];
        } else {
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
                [keyboard addSubview:doneButton];
        }
    }
}

- (void)doneButtonClicked:(id)Sender {
//Write your code whatever you want to do on done button tap
//Removing keyboard or something else
}
Run Code Online (Sandbox Code Playgroud)

我在我的应用程序中使用相同的按钮的框架因此被调整,因此您可以在需要在键盘上显示完成按钮时调用[self addButtonToKeyboard].否则UIKeyboardTypeNumberPad没有完成按钮.在此输入图像描述