Search code examples
iphoneobjective-cuipickerviewuitoolbar

UIToolBar is not shown when adding the same toolbar as inputAccessaryView


I have got a uipickerview which will be populated when pressing a uibutton. This uipickerview has a uitoolbar control in the top of the uipickerview. That tool bar has got uibuttons in it.It is working fine until i do the below thing.

I want to have that uittoolbar, on top of the uikeyboard.So I have added , that uitoolbar control as InputAccessoryView. When i did this, the uitoolbar comes with uikeyboard.that is fine.

but when i tap on the button, which has to show the uipickerview with that toolbar and also needs to resign the toolbar.

So when tap on the button, i have resigned the uikeyboard. the uipickerview is showing but the toolbar is not visible.

I have been trying to fix this for a long time, but really facing a tough time.

Please let me know.

- (void)viewDidLoad
{
[super viewDidLoad];

// Create an UIButton
self.btnClick = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[self.btnClick addTarget: self action:@selector(showPicker) 
    forControlEvents:UIControlEventTouchUpInside]; 
// Set frame width, height
self.btnClick.frame = CGRectMake(10, 100, 30, 30);    
self.btnClick.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:self.btnClick];

self.toolBar = [[[UIToolbar alloc] init]autorelease];
[self.toolBar sizeToFit];
self.toolBar.frame = CGRectMake(0,198, 320, 35);
self.toolBar.barStyle = UIBarStyleBlackTranslucent;

UIImage *image = [UIImage imageNamed:@"orangebuttonsmall.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.bounds = CGRectMake( 0, 0, image.size.width, 25 );    
[aButton setImage:image forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(pickerNumPadClicked:) forControlEvents:UIControlEventTouchUpInside];    
UIBarButtonItem *numButton = [[[UIBarButtonItem alloc] initWithCustomView:aButton]autorelease];

UIImage *aCenterimage = [UIImage imageNamed:@"button_normal.png"];
UIButton *myCenterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[myCenterBtn setTitle:@"Poet Names" forState:UIControlStateNormal];
[myCenterBtn setBackgroundImage:aCenterimage forState:UIControlStateNormal];
myCenterBtn.frame = CGRectMake(100, 0,200,25);
myCenterBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myCenterBtn.titleLabel.font = [UIFont systemFontOfSize:10];
myCenterBtn.titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 
myCenterBtn.titleLabel.shadowOffset = CGSizeMake(0, -1);
myCenterBtn.bounds = CGRectInset(myCenterBtn.bounds, -3, 2);
myCenterBtn.titleLabel.font = [UIFont boldSystemFontOfSize:13];
[myCenterBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myCenterBtn addTarget:self action:@selector(aCenterBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *flex = [[[UIBarButtonItem alloc] initWithCustomView:myCenterBtn]autorelease];

UIImage *sendImage = [UIImage imageNamed:@"orangebuttonsmall.png"];
UIButton *Sendbutton = [UIButton buttonWithType:UIButtonTypeCustom];
Sendbutton.frame = CGRectMake(0, 0,50,25);
[Sendbutton setTitle:@"Send" forState:UIControlStateNormal];

[Sendbutton setBackgroundImage:sendImage forState:UIControlStateNormal];
Sendbutton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
Sendbutton.titleLabel.font = [UIFont systemFontOfSize:10];
Sendbutton.titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 
Sendbutton.titleLabel.shadowOffset = CGSizeMake(0, -1);
Sendbutton.bounds = CGRectMake( 0, 0, 50, 25 );   
Sendbutton.titleLabel.font = [UIFont boldSystemFontOfSize:13];

[Sendbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[Sendbutton addTarget:self action:@selector(pickerDoneClicked:) forControlEvents:UIControlEventTouchUpInside];    
UIBarButtonItem *doneButton = [[[UIBarButtonItem alloc ] initWithCustomView:Sendbutton]autorelease];

[self.toolBar setItems:[NSArray arrayWithObjects:numButton,flex, doneButton, nil]];
[self.view addSubview:self.toolBar];

self.txtView = [[UITextView alloc] init];
self.txtView.layer.cornerRadius = 15;;
self.txtView.clipsToBounds = YES;
self.txtView.frame = CGRectMake(45, 100, 274, 98);
self.txtView.layer.borderWidth = 1.0f;
self.txtView.delegate = self;
self.txtView.scrollEnabled = YES;
self.txtView.backgroundColor = [UIColor whiteColor];
self.txtView.contentInset = UIEdgeInsetsZero;
self.txtView.returnKeyType = UIReturnKeyDone;  // type of the return key
self.txtView.layer.borderColor = [[UIColor blueColor] CGColor];
self.txtView.font = [UIFont fontWithName:@"Helvetica" size:17.0f];
[self.txtView setInputAccessoryView:self.toolBar];
[self.view addSubview:self.txtView];

self.itemsArray = [[[NSArray alloc]initWithObjects:@"William Langland",@"Philip Larkin", @"Dorianne Laux", @"David Lehman", @"Denise Levertov", nil]autorelease];

}

-(void)showPicker
{
[self.txtView resignFirstResponder];

self.pickerView = [[[UIPickerView alloc]init]autorelease];
self.pickerView.showsSelectionIndicator = YES;
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
self.pickerView.tag = 0;
self.pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                                        UIViewAutoresizingFlexibleHeight);
[self.pickerView sizeToFit];

self.pickerView.frame = CGRectMake(0, 293 , 320, 15);

self.toolBar.frame = CGRectMake(0,253, 320, 35);

[self.view addSubview:self.toolBar];
[self.view addSubview:self.pickerView]; 
[self.pickerView selectRow:1 inComponent:0 animated:YES];
}

Here the toolbar is shown using inputAccessaryView

Here the toolbar is not shown


Solution

  • Try this link

    http://blog.swierczynski.net/2010/12/how-to-create-uipickerview-with-toolbar-above-it-in-ios/

    It shows how to put a inputAccessoryView on uipicker