0

我是 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。

4

2 回答 2

2

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

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

这是我的一个应用程序中的一些示例代码:

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

编辑:该代码用于说明目的,如果您只是将其复制粘贴到您的应用程序中,它可能不会立即起作用。

于 2013-05-29T15:09:17.403 回答
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;
        }   
    }
}
于 2013-05-30T06:15:53.433 回答