开发者

UIToolbar not adding to Keyboard

开发者 https://www.devze.com 2023-03-12 19:46 出处:网络
I am trying to add a UIToolbar to the UIKeyboard so that the username can switch easily between two text fields. However I cannot get it to work. Please can you tell me what I am doing wrong. I have s

I am trying to add a UIToolbar to the UIKeyboard so that the username can switch easily between two text fields. However I cannot get it to work. Please can you tell me what I am doing wrong. I have set up the NotificationCenter in my 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 = CG开发者_运维问答RectMake(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);
                    }
                }
            }
        }
    }
} 


Create an instance of UIToolbar, Add whichever views you required in toolbar then assign your toolbar instance in inputAccessoryView property of UITextField.

myTextFieldorTextView.inputAccessoryView = boolbar;

Check the below step by step guide to put an toolbar just top of keyboard.

Adding a Toolbar with Next & Previous above UITextField Keyboard

0

精彩评论

暂无评论...
验证码 换一张
取 消