如何在ios 9中的iPhone数字键盘上显示“完成”按钮

时间:2015-10-31 00:35:14

标签: ios9 numpad

- (void)keyboardWillShow:(NSNotification *)note {
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    //[doneButton setImage:[UIImage imageNamed:@"doneButtonNormal.png"] forState:UIControlStateNormal];
    //[doneButton setImage:[UIImage imageNamed:@"doneButtonPressed.png"] forState:UIControlStateHighlighted];
    [doneButton setTitle:@"完成" forState:UIControlStateNormal];
    [doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];


    UIView *foundKeyboard = nil;
    UIWindow *keyboardWindow = nil;


    keyboardWindow = [[[UIApplication sharedApplication] windows] lastObject] ;
    keyboardWindow.userInteractionEnabled = YES;
    if (!keyboardWindow) return;

    for (__strong UIView *possibleKeyboard in [keyboardWindow subviews]) {

        if ([[possibleKeyboard description] hasPrefix:@"<UIInputSetContainerView"]) {
            for (__strong UIView *possibleKeyboard_2 in possibleKeyboard.subviews) {
                if ([possibleKeyboard_2.description hasPrefix:@"<UIInputSetHostView"]) {
                    foundKeyboard = possibleKeyboard_2;
                }
            }
        }
    }


    if (foundKeyboard) {
        if ([[foundKeyboard subviews] indexOfObject:doneButton] == NSNotFound) {
            [foundKeyboard addSubview:doneButton];
        } else {
            [foundKeyboard bringSubviewToFront:doneButton];
        }

    }

}

1 个答案:

答案 0 :(得分:0)

试试这可能会有所帮助。

- (void)viewDidLoad
    {
        [super viewDidLoad];

        UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
        numberToolbar.barStyle = UIBarStyleBlackTranslucent;
        numberToolbar.items = [NSArray arrayWithObjects:
                             [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
                             [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                             [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                         nil];
        [numberToolbar sizeToFit];
        numberTextField.inputAccessoryView = numberToolbar;
    }

    -(void)cancelNumberPad{
        [numberTextField resignFirstResponder];
        numberTextField.text = @"";
    }

    -(void)doneWithNumberPad{
        NSString *numberFromTheKeyboard = numberTextField.text;
        [numberTextField resignFirstResponder];
    }