2012-10-04 25 views
7

Ho aggiunto una vista con i pulsanti sulla parte superiore della tastiera utilizzando setInputAccessoryView. Ora voglio funzionalità come quando faccio clic sul pulsante dalla vista che voglio visualizzare un picker View sulla parte superiore della tastiera. Significa aggiungere pickerView come subview della tastiera.Come aggiungere una vista sulla parte superiore della tastiera?

Come posso fare questo?

+0

hai qualche esempio di app che fanno questo? – Jessedc

risposta

1

È necessario creare un altro UIWindow prima:

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]; 'Ma non funziona. – user1244311

+0

descrivere: non funziona .. :) –

1

come il tasto aggiungi sulla tastiera è possibile aggiungere anche vista come muggito ...

Trova notizie finestra della tastiera e quindi impostare cornice del pulsante e anche yourView e quindi aggiungere alla tastiera

- (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 
    } 
} 

spero che questo aiuto si ...

:)

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]; 

E in ShowPicker Metodo, scrivere il codice per la visualizzazione UIPicker

+0

Il mio problema è che posso aggiungere PickerView come subView della mia vista ma la tastiera è nascosta. quindi cosa voglio quando clicco sul pulsante voglio visualizzare la vista di selezione sulla parte superiore della tastiera. – user1244311

+0

il tuo problema è che la tastiera non è visualizzata e vuoi vedere il pickerview sul tuo keyboed ??? ho ragione ?? –

+0

Ho aggiunto una vista sulla parte superiore della tastiera. quella vista ha pulsanti. quando clicco su un pulsante voglio visualizzare una vista di selezione al posto della tastiera. ma il problema è che la vista è aggiunta alla tastiera. quindi se nascondi la tastiera, anche quella dei pulsanti sarà nascosta. – user1244311

19

Fai un vista che si desidera nella parte superiore della tastiera. Puoi farlo da xib o manualmente, ma penso che xib sarà un'opzione migliore.

poi fare il riferimento a questo punto di vista in questo modo

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

Ogni volta che si desidera nascondere questo o rimuovere questo basta mettere questo

textField.inputAccessoryView = nil; 
0

Dalla risposta di 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]; 

Basta cambiare UIWindowLevelStatusBar a CGFLOAT_MAX sarà ok.

Problemi correlati