UIToolbar не добавляется на клавиатуру - PullRequest
0 голосов
/ 11 июня 2011

Я пытаюсь добавить UIToolbar на UIKeyboard, чтобы имя пользователя могло легко переключаться между двумя текстовыми полями. Однако я не могу заставить его работать. Пожалуйста, вы можете сказать мне, что я делаю неправильно. Я настроил Центр уведомлений в моем viewDidLoad.

- (void)keyboardWillShow:(NSNotification *)notification {
    for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
        // Now iterating over each subview of the available windows
        for (UIView *keyboard in [keyboardWindow subviews]) {
            // Check to see if the description of the view we have referenced is UIKeyboard.
            // If so then we found the keyboard view that we were looking for.
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) {
                NSValue *v = [[notification userInfo] valueForKey:UIKeyboardBoundsUserInfoKey];
                CGRect kbBounds = [v CGRectValue];
                UIToolbar *input = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kbBounds.size.width, 50)];
                UIBarButtonItem *previous = [[UIBarButtonItem alloc] initWithTitle:@"Previous" style:UIBarButtonItemStylePlain target:self action:@selector(previousTextField:)];
                UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(nextTextField:)];
                UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
                UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(hideKeyboard:)];
                [input setItems:[NSArray arrayWithObjects:previous,next,space,done,nil]];
                if(input == nil) {
                    UIToolbar *input = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kbBounds.size.width, 50)];
                    UIBarButtonItem *previous = [[UIBarButtonItem alloc] initWithTitle:@"Previous" style:UIBarButtonItemStylePlain target:self action:@selector(previousTextField:)];
                    UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(nextTextField:)];
                    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
                    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(hideKeyboard:)];
                    [input setItems:[NSArray arrayWithObjects:previous,next,space,done,nil]];
                }
                [input removeFromSuperview];
                input.frame = CGRectMake(0, 0, kbBounds.size.width, 30);
                [keyboard addSubview:input];
                keyboard.bounds = CGRectMake(kbBounds.origin.x, kbBounds.origin.y, kbBounds.size.width, kbBounds.size.height + 60);
                for(UIView* subKeyboard in [keyboard subviews]) {
                    if([[subKeyboard description] hasPrefix:@"<UIKeyboardImpl"] == YES) {
                        subKeyboard.bounds = CGRectMake(kbBounds.origin.x, kbBounds.origin.y - 30, kbBounds.size.width, kbBounds.size.height);
                    }
                }
            }
        }
    }
} 

1 Ответ

2 голосов
/ 11 июня 2011

Создайте экземпляр UIToolbar, добавьте все необходимые вам виды на панели инструментов, затем присвойте свой экземпляр панели инструментов в inputAccessoryView свойстве UITextField.

myTextFieldorTextView.inputAccessoryView = boolbar;

Посмотрите приведенное ниже пошаговое руководство, чтобы расположить панель инструментов над клавиатурой.

Добавление панели инструментов со следующей и предыдущей клавиатурой UITextField

...