iPad框架上的UIActionSheet太小了

时间:2020-12-31 20:21:22

I am trying to display a UIActionSheet on the iPad with a UIPickerView in it, I have the equivalent code working for iPhone so my UIPickerView delegate and datasource stuff work, but on the iPad when I use -[UIActionSheet showFromRect:inView:animated:] the result UIPopover/UIActionSheet is way too small and I can not seem to set the frame size, also none of the buttons are displayed.

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

I don't know if this is because they are outside the bounds or there is something else going on. This is what my code looks like after I have removed all non-essential code (iPhone etc). Does anybody know what I am doing wrong, does anybody know of any examples.

我不知道这是因为它们是在界限之外还是还有其他事情发生。这是我删除所有非必要代码(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 个解决方案

#1


18  

I think that UIActionSheet is not resizable, try to comment in your code the line with [pickerActionSheet addSubview:thePickerView]; and you will see that the ActionSheet fits perfecly to the buttons.

我认为UIActionSheet不可调整大小,尝试用你的代码注释[pickerActionSheet addSubview:thePickerView];并且您将看到ActionSheet完全适合按钮。

I would recommend a UIPopoverController with a custom UIViewController. Something like this:

我推荐一个带有自定义UIViewController的UIPopoverController。像这样的东西:

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

Where popover is a class variable declared in .h (UIPopoverController *popover;).

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

By the way, I'm using ARC, so there is no release in the code.

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

#2


8  

I managed to make it work using a silly way.

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

For your reference:
Setup PickerView.

UIActionSheet's width can't be adjusted somehow, so have to adjust pickerView accordingly. Height wise, you can adjust with the amount of "\n" in actionSheet title.

供您参考:安装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 or Bound doesn't work for me.

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

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



Agreed on using UIPopoverController, but don't know why, it got a serious UI delay when I put UIDatePickerView into popover (it took almost 1 sec lag to pop up) which I can't find the root cause. So have to fallback to above method.


Happy Coding.

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

#1


18  

I think that UIActionSheet is not resizable, try to comment in your code the line with [pickerActionSheet addSubview:thePickerView]; and you will see that the ActionSheet fits perfecly to the buttons.

我认为UIActionSheet不可调整大小,尝试用你的代码注释[pickerActionSheet addSubview:thePickerView];并且您将看到ActionSheet完全适合按钮。

I would recommend a UIPopoverController with a custom UIViewController. Something like this:

我推荐一个带有自定义UIViewController的UIPopoverController。像这样的东西:

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

Where popover is a class variable declared in .h (UIPopoverController *popover;).

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

By the way, I'm using ARC, so there is no release in the code.

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

#2


8  

I managed to make it work using a silly way.

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

For your reference:
Setup PickerView.

UIActionSheet's width can't be adjusted somehow, so have to adjust pickerView accordingly. Height wise, you can adjust with the amount of "\n" in actionSheet title.

供您参考:安装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 or Bound doesn't work for me.

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

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



Agreed on using UIPopoverController, but don't know why, it got a serious UI delay when I put UIDatePickerView into popover (it took almost 1 sec lag to pop up) which I can't find the root cause. So have to fallback to above method.


Happy Coding.

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