如何找到iOS6的键盘视图

时间:2013-05-29 15:00:59

标签: objective-c ios6 uikeyboard

我是iOS新手,我想知道如何在iOS6中找到键盘视图,因为我想在数字键盘键盘上添加自定义按钮, 我使用这段代码:

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

适用于iOS 5,但在iOS 6中无效。

2 个答案:

答案 0 :(得分:2)

您应该使用inputAccessoryView(或类似控件)的UITextField属性。来自文档:

  

此属性的默认值为nil。想要将自定义控件附加到系统提供的输入视图(例如键盘)或自定义输入视图(您在inputView属性中提供的视图)的子类应将此属性重新声明为readwrite并使用它来管理其自定义附件视图。当接收器随后成为第一响应者时,响应者基础结构在显示之前将视图附加到适当的输入视图。

以下是我的某个应用中的一些示例代码:

// 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;

编辑:该代码仅用于说明目的,如果您只是将其复制粘贴到您的应用中,它可能无法立即生效。

答案 1 :(得分:0)

可能的键盘不必是iOS 6中子视图中的第一个。尝试替换这些行:

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

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

有了这些:

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