iPad框架上的UIActionSheet太小了

Nat*_*Day 13 cocoa-touch objective-c ipad ios

我试图UIActionSheet在iPad上显示一个内置的UIPickerView,我有相同的代码适用于iPhone,所以我的UIPickerView代理和数据源的东西工作,但在我使用-[UIActionSheet showFromRect:inView:animated:]结果的iPad上UIPopover/ UIActionSheet太小,我似乎无法设置帧大小,也不显示任何按钮.

我不知道这是因为它们是在界外还是还有其他事情发生.这是我删除所有非必要代码(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;
Run Code Online (Sandbox Code Playgroud)

小智 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];
Run Code Online (Sandbox Code Playgroud)

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

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


Jap*_*Con 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];
Run Code Online (Sandbox Code Playgroud)

设置框架或绑定对我不起作用.

[actionSheet setBounds:pickerView.frame];   
[actionSheet setFrame:pickerView.frame];
Run Code Online (Sandbox Code Playgroud)



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


快乐的编码.