通过按钮点击加载PickerView太小

时间:2012-05-09 23:06:56

标签: objective-c ipad uipickerview uipopovercontroller uiactionsheet

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

- (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行)并且我正在显示的工具栏项都没有渲染。

see picture

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

1 个答案:

答案 0 :(得分: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链接,但我认为这只是一个选择器视图的复杂。