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

    }

}
4

1 回答 1

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];
    }
于 2015-10-31T01:15:18.017 回答