2012-10-04 47 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即可。

相关问题