[英]How to add a view on top of keyboard?
我使用setInputAccessoryView
在鍵盤頂部添加了一個帶按鈕的視圖。 現在我想要的功能就像當我從視圖中單擊按鈕時我想在鍵盤上顯示一個pickerView 。 將pickerView添加為鍵盤子視圖的方法。
我怎樣才能做到這一點?
在鍵盤頂部創建您想要的視圖。 您可以從xib或手動執行此操作,但我認為xib將是更好的選擇。
然后通過執行此操作來引用此視圖
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
textField.inputAccessoryView = YOURCustomView;
return YES;
}
無論何時你想隱藏這個或刪除它只是把它
textField.inputAccessoryView = nil;
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];
在ShowPicker方法中,編寫顯示UIPicker的代碼
您需要先創建另一個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];
喜歡在鍵盤上添加按鈕,你也可以添加像下面的視圖...
在這里你找到鍵盤的窗口,然后設置按鈕框架和你的視圖,然后添加到鍵盤
- (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
}
}
我希望這能幫到你......
:)
來自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];
只需將UIWindowLevelStatusBar
更改為CGFLOAT_MAX
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.