How to add a top view of the keyboard?

I added a view with buttons on the keyboard using setInputAccessoryView . Now I want functionality, for example, when I click on a button in the view that I want to display pickerView on top of the keyboard. Tools such as adding pickerView as a keyboard spy.

How can i do this?

+7
source share
5 answers

Make the look you want at the top of the keyboard. You can do this from xib or manually, but I think xib would be a better option.

Then make a link to this view by doing this

  - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { textField.inputAccessoryView = YOURCustomView; return YES; } 

Whenever you want to hide it or delete it, just put it

 textField.inputAccessoryView = nil; 
+19
source
 self.pickerContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 194, 320, 224)]; self.pickerContainerView.backgroundColor = [UIColor clearColor]; [self.view addSubview:self.pickerContainerView]; UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; pickerToolbar.barStyle = UIBarStyleBlackTranslucent; [pickerToolbar sizeToFit]; self.lblPickerViewTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 7, 230, 30)]; self.lblPickerViewTitle.backgroundColor = [UIColor clearColor]; [self.lblPickerViewTitle setFont: [UIFont fontWithName:@"Arial-BoldMT" size:17]]; self.lblPickerViewTitle.textAlignment = UITextAlignmentLeft; self.lblPickerViewTitle.textColor =[UIColor whiteColor]; [pickerToolbar addSubview:self.lblPickerViewTitle]; NSMutableArray *barItems = [[NSMutableArray alloc] init]; UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; [barItems addObject:flexSpace]; [flexSpace release]; UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(closePickerView:)]; [barItems addObject:btnCancel]; [btnCancel release]; UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:@"ShowPicker" style:UIBarButtonItemStyleBordered target:self action:@selector(donePickerView:)]; [barItems addObject:btnDone]; [btnDone release]; [pickerToolbar setItems:barItems animated:YES]; [barItems release]; [self.pickerContainerView addSubview:pickerToolbar]; 

And in ShowPicker , write code to display UIPicker

+2
source

First you need to create another UIWindow:

 UIWindow *newWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0,100,320,20)]; newWindow.windowLevel = UIWindowLevelStatusBar; // this will make to place your WINDOW above the keyboard [newWindow makeKeyAndVisible]; // show the new window // [newWindow addSubview:yourView]; 
+1
source

as an add button on the keyboard, you can also add a view similar to the one below ...

here you will find the keyboard window, and then set the button frame as well as your screen, and then add to the keyboard

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; return YES; } - (void)keyboardDidShow:(NSNotification *)note { UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeCustom]; returnBtn.frame = CGRectMake(0,-25,320,25); returnBtn.adjustsImageWhenHighlighted = NO; returnBtn.backgroundColor=[UIColor darkGrayColor]; returnBtn.titleLabel.textColor=[UIColor whiteColor]; [returnBtn setBackgroundImage:[UIImage imageNamed:@"keyBtn.png"] forState:UIControlStateNormal]; [returnBtn addTarget:self action:@selector(keyboardBtn:) forControlEvents:UIControlEventTouchUpInside]; // locate keyboard view UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; UIView* keyboard; for(int i=0; i<[tempWindow.subviews count]; i++) { keyboard = [tempWindow.subviews objectAtIndex:i]; // keyboard found, add the button if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) // if (txtTag==5) { [keyboard addSubview:returnBtn]; } } -(IBAction)keyboardBtn:(id)sender{ UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; UIView* keyboard; for(int i=0; i<[tempWindow.subviews count]; i++) { keyboard = [tempWindow.subviews objectAtIndex:i]; // keyboard found, add the button if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) // if (txtTag==5) { [keyboard addSubview:yourView];// here add your view with frame } } 

Hope this helps you ...

:)

+1
source

From the answer of Jonas Schnelli

 UIWindow *newWindow = [[UIWindow alloc]initWithFrame:CGRectMake(0,100,320,20)]; newWindow.windowLevel = CGFLOAT_MAX; // this will make to place your WINDOW above the keyboard [newWindow addSubview:yourView]; 

Just change the UIWindowLevelStatusBar to CGFLOAT_MAX will be ok.

0
source

All Articles