2

我正在构建一个 iPad 应用程序,我希望在用户按下按钮(常规按钮,而不是 ToolBarItem)时弹出一个 uipickerview。我意识到通常你会从工具栏上做弹出框类型的东西,但在这种情况下,我需要它发生在标准的按钮点击上。我做了很多搜索,这是我能够想出的代码(这是按钮单击的代码):

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

}

这似乎确实是在弹出窗口中创建选择器控件,但是该控件非常小(仅显示大约 1 行),并且我显示的工具栏项目都没有呈现。

看图片

如何控制大小?我尝试调整正在创建的两个 CGRect 对象的值,但这似乎没有太大区别。

4

1 回答 1

0

在那种情况下,我总是使用 UIPopoverController。您可以在 IB 中构建一个视图,其中包含您想要的大小的选择器,然后将此视图添加到 popoverController 并从您想要的任何内容(文本字段、工具栏等)呈现它。

UPD:示例代码

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

另一种解决方案是在 IB 中创建一个新的 UIViewController 并将其与 popover segue 链接,但我认为对于一个 pickerview 来说它很复杂。

于 2012-05-10T06:38:30.697 回答