Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActionSheet on iPad frame too small

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.

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.

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;
like image 220
Nathan Day Avatar asked Nov 02 '11 00:11

Nathan Day


2 Answers

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.

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

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;).

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

like image 64
Esepakuto Avatar answered Oct 01 '22 10:10

Esepakuto


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.

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.

like image 31
JapCon Avatar answered Oct 01 '22 10:10

JapCon