iPad框架上的UIActionSheet太小了

时间:2011-11-02 00:18:10

标签: objective-c ios cocoa-touch ipad

我正在尝试在iPad上显示UIActionSheet,其中包含UIPickerView,我有相同的代码适用于iPhone,因此我的UIPickerView委托和数据源工作正常,但是当我使用-[UIActionSheet showFromRect:inView:animated:]时,结果UIPopover / UIActionSheet的iPad太小了,我似乎无法设置框架大小,也没有显示任何按钮。

我不知道这是因为他们是在界外还是还有其他事情发生。这是我删除所有非必要代码(iPhone等)后代码的样子。有人知道我做错了什么,有没有人知道任何例子。

CGRect thePickerFrame = CGRectMake(0, 0, 320.0, 485.0);
UIPickerView * thePickerView = [[UIPickerView alloc] initWithFrame:thePickerFrame];

[pickerActionSheet release], pickerActionSheet =
        [[UIActionSheet alloc] initWithTitle:@"Choose" delegate:self 
                               cancelButtonTitle:@"Cancel"
                               destructiveButtonTitle:nil
                               otherButtonTitles:@"Next", nil];
thePickerView.showsSelectionIndicator = YES;
thePickerView.dataSource = self;
thePickerView.delegate = self;

[pickerActionSheet addSubview:thePickerView];
[thePickerView selectRow:0 inComponent:0 animated:NO];
[thePickerView release];

[pickerActionSheet showFromRect:currentTextField.bounds
                             inView:currentTextField animated:NO];
pickerActionSheet.frame = thePickerFrame;

2 个答案:

答案 0 :(得分:18)

我认为UIActionSheet不可调整大小,尝试在代码中使用[pickerActionSheet addSubview:thePickerView];注释,你会看到ActionSheet完全符合按钮。

我建议UIPopoverController使用自定义UIViewController。像这样:

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolbar.barStyle = UIBarStyleDefault;

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(BACK_ACTION:)];
UIBarButtonItem *chooseButton = [[UIBarButtonItem alloc] initWithTitle:@"Choose" style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(NEXT_ACTION:)];
UIBarButtonItem *fixed1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *fixed2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

[toolbar setItems:[NSArray arrayWithObjects:cancelButton, fixed1, chooseButton, fixed2, nextButton, nil]];

UIPickerView    *thePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
CGRect thePickerFrame = thePickerView.frame;
thePickerFrame.origin.y = toolbar.frame.size.height;
[thePickerView setFrame:thePickerFrame];


UIView *view = [[UIView alloc] init];
[view addSubview:thePickerView];
[view addSubview:toolbar];

UIViewController *vc = [[UIViewController alloc] init];
[vc setView:view];
[vc setContentSizeForViewInPopover:CGSizeMake(320, 260)];

popover = [[UIPopoverController alloc] initWithContentViewController:vc];

thePickerView.showsSelectionIndicator = YES;
thePickerView.dataSource = self;
thePickerView.delegate = self;

[thePickerView selectRow:0 inComponent:0 animated:NO];

[popover presentPopoverFromRect:currentTextField.bounds inView:currentTextField permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

其中popover是在.h(UIPopoverController *popover;)中声明的类变量。

顺便说一下,我正在使用ARC,因此代码中没有发布。

答案 1 :(得分:8)

我设法使用愚蠢的方式使其工作。

供参考:
设置PickerView。

UIActionSheet的宽度无法以某种方式调整,因此必须相应地调整pickerView。高度方面,您可以在actionSheet标题中调整“\ n”的数量。

UIDatePicker * pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 270, 220)];
pickerView.datePickerMode = UIDatePickerModeDate; 

UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n\n\n" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];    

[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[actionSheet addSubview:pickerView];
[actionSheet showFromRect:button.frame inView:button.superview animated:YES];
[actionSheet release];

Set Frame或Bound对我不起作用。

[actionSheet setBounds:pickerView.frame];   
[actionSheet setFrame:pickerView.frame];



同意使用UIPopoverController,但不知道为什么,当我把UIDatePickerView放入popover时它有一个严重的UI延迟(它弹出了差不多1秒的滞后),我无法找到根本原因。所以必须回到上面的方法。


快乐的编码。