I have a UIPickerview that is created in interface builder. And via code I placed a toolbar as its subview and a done button as the toolbars bar button item. I want to execute an action when the done button is clicked (hide the pickerview and some extra stuff). But for some reason the action for the done button is not working. Here is the code to create the toolbar and the bar button item in viewDidLoad.
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
toolBar.barTintColor = [UIColor colorWithRed:70/250.0f green:70/250.0f blue:70/250.0f alpha:1.0];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonTapped:)];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
toolBar.items = @[flexible, barButtonDone, flexible];
barButtonDone.tintColor=[UIColor colorWithRed:227/255.0f green:178/255.0f blue:49/255.0f alpha:1.0];
[picker addSubview:toolBar];
[picker bringSubviewToFront:toolBar];
And here is the method doneButtonTapped:
-(void)doneButtonTapped:(UIBarButtonItem *)sender{
NSLog(@"Done button tapped");
picker.hidden = true;
[self.view bringSubviewToFront:txtCity];
[self.view bringSubviewToFront:advancedSearchLine];
[self.view bringSubviewToFront:txtCityLine];
}
I checked in layout debugger while running the app and indeed the done button is in the front of the view, so nothing in front of it to prevent it from working. What am I doing wrong?
you cannot add toolbar as subview of picker view.create a container in UIView add toolbar and picker as subview.
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, toolBar.frame.size.height + picker.frame.size.height)];
inputView.backgroundColor = [UIColor clearColor];
[inputView addSubview:picker];
[inputView addSubview:toolBar];