I'm building an iPad app and I wish to popup a uipickerview when the user presses a button (a regular button, not a ToolBarItem). I realize that typically you do popover type things from a toolbar but in this instance I need it to happen on a standard button click. I've done quite a bit of search and this is the code I was able to come up with (this is the code for the Button click):
- (IBAction)showTagPicker:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select a Category" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect frame = CGRectMake(0, 40, 320, 450);
PCCategory *categories = [[PCCategory alloc] init];
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:frame];
picker.delegate = categories;
picker.dataSource = categories;
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 464)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(tagSelected)];
[barItems addObject:doneButton];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];
[actionSheet addSubview:picker];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, 320, 464)];
}
This does seem to be creating the picker control in a popover, however the control is very small (its only displaying about 1 line) and none of the tool bar items I'm showing are rendering.
How can I control the size? I've tried tweaking the values of the two CGRect objects that are being created but that doesn't seem to make much of a difference.
In that sort of cases i always use UIPopoverController. You can build a view in IB with the picker inside with the size you want and then add this view to the popoverController and present it from whatever you want (textfield, toolbar, etc).
UPD: Example code
UIViewController *vc = [UIViewController new];
vc.view = yourview; //With whatever you want in it
vc.contentSizeForViewInPopover = yourview.frame.size;
vc.navigationItem.title = @"Title"; // If you want one
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:vc];
popover presentPopoverFromRect:rect inView: self.view premittedArrowDirections:UIPopoverArrowDirectionAny animated: YES];
//rect is a frame from which you would like to present your popover. For example: yourUIButton.frame
if (yourview.hidden)
[yourview setHidden:NO];
Another solution is to create a new UIViewController in IB and link it with popover segue but I think it's to complex for just one pickerview.