如何定位键盘视图iOS6



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

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

此属性的默认值为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;

编辑:代码是为了说明的目的,它可能不会立即工作,如果你只是复制粘贴到你的应用程序。

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

最新更新