2012-10-04 55 views
7

我使用setInputAccessoryView在鍵盤頂部添加了一個按鈕視圖。 現在我想要的功能就像當我點擊按鈕從視圖中我想要顯示pickerView在鍵盤上。意味着像添加pickerView作爲鍵盤的子視圖。如何在鍵盤上添加視圖?

我該怎麼做?

+0

你有這樣做的應用程序的任何例子? – Jessedc

回答

1

您需要先創建另一個的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]; 
+0

'UIWindow * newWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0.0,250,320,200)]; newWindow.windowLevel = UIWindowLevelStatusBar; [newWindow makeKeyAndVisible]; [newWindow addSubview:self.myView];'但它不工作。 – user1244311

+0

描述:它不工作.. :) –

1

想添加鍵盤按鈕,你還可以添加視圖像波紋管......

這裏你會發現鍵盤的窗口,然後設置按鈕的框架也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 
    } 
} 

我希望這會幫助你...

:)

2
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

+0

我的問題是我可以添加pickerView作爲我的看法的子視圖,但鍵盤是隱藏的。所以當我點擊按鈕時,我想要在鍵盤上顯示選取器視圖。 – user1244311

+0

你的問題是鍵盤不顯示,你要在我的鍵盤上顯示pickerview ???我對嗎? – 2012-10-04 07:01:37

+0

我在鍵盤上添加了一個視圖。該視圖具有按鈕。當我點擊一個按鈕,我想顯示一個選擇器視圖,而不是鍵盤。但問題是該視圖被添加到鍵盤。所以如果你隱藏鍵盤那個按鈕的視圖也會被隱藏起來。 – user1244311

19

代碼讓你想一個視圖在鍵盤的頂部。你可以從xib或手動完成,但我認爲xib將是更好的選擇。

然後做這個

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

每當你想隱藏這個或刪除這只是把這個

textField.inputAccessoryView = nil; 
0

從喬納斯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即可。

相關問題