محمود مندور
محمود مندور

Reputation: 23

How to Locate the keyboard view iOS6

I am new in iOS, and I want to know how to locate the keyboard view in iOS6 as I want to add a custom button to number pad keyboard, I use this code :

UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
    if (![[testWindow class] isEqual:[UIWindow class]]) {
        keyboardWindow = testWindow;
        break;
    }
}

//locate the keyboard
UIView *foundKeyboard = nil;
for (UIView  __strong *possibleKeyboard in [keyboardWindow subviews]) {

    // iOS 4 sticks the UIKeyboard inside a UIPeripheralHostView.
    if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
        possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0];
    }

    if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"]) {
        foundKeyboard = possibleKeyboard;
        break;
    }
}

if (foundKeyboard) {
    // Add the button to foundKeyboard.
    [foundKeyboard addSubview:doneButton];
}

it works fine with iOS 5 but not works in iOS 6.

Upvotes: 0

Views: 544

Answers (2)

Valent Richie
Valent Richie

Reputation: 5226

The possible keyboard is not necessary to be the first in the subviews in iOS 6. Try to replace these lines:

if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
    possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0];
}

if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"]) {
    foundKeyboard = possibleKeyboard;
    break;
}

With these:

if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
    for (__strong UIView *anotherPossibleKeyboard in [possibleKeyboard subviews]) {
        if ([[anotherPossibleKeyboard description] hasPrefix:@"<UIKeyboard"]) {
            foundKeyboard = possibleKeyboard;
            break;
        }   
    }
}

Upvotes: 0

nikolovski
nikolovski

Reputation: 4049

You should probably use the inputAccessoryView property of a UITextField (or similar control). From the docs:

The default value of this property is nil. Subclasses that want to attach custom controls to either a system-supplied input view (such as the keyboard) or a custom input view (one you provide in the inputView property) should redeclare this property as readwrite and use it to manage their custom accessory view. When the receiver subsequently becomes the first responder, the responder infrastructure attaches the view to the appropriate input view before displaying it.

Here is some sample code from one of my apps:

// Adding a UIToolbar on top of the keyboard
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
keyboardDoneButtonView.tintColor = nil;
keyboardDoneButtonView.barStyle = UIBarStyleBlack;
keyboardDoneButtonView.translucent = YES;
[keyboardDoneButtonView sizeToFit];

// Done button on the left
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(editingDone:)];
doneButton.tintColor = TINT_COLOR;
// Creating a flexible space so the button is on the right side
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
// Creating the Next button on top of the keyboard
UISegmentedControl *saveButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Previous", @"Next", nil]];
saveButton.momentary = YES; 
saveButton.frame = CGRectMake(260, 7.0f, 120.0f, 30.0f);
saveButton.segmentedControlStyle = UISegmentedControlStyleBar;
saveButton.tintColor = [UIColor blackColor];
[saveButton addTarget:self action:@selector(moveToNextOrPrevious:) forControlEvents:UIControlEventValueChanged];
saveButton.tag = indexPath.row;
// You have to encapsulate the SegCtrl in a bar button item for it to work
UIBarButtonItem *segCtrlEnc = [[UIBarButtonItem alloc] initWithCustomView: saveButton];

// Adding the toolbar with elements
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects: segCtrlEnc, flexibleSpace, doneButton, nil]];

// Plug the keyboardDoneButtonView into the text field...
cell0tf.textField.inputAccessoryView = keyboardDoneButtonView;

EDIT: The code is for illustrative purposes, it's probably not going to work immediately if you just copy-paste it into your app.

Upvotes: 2

Related Questions