I have a UIPickerView
displayed inside a UIPopoverController
. The dimensions of the UIPickerView
are: 320x216
. For some reason, the UIPickerView
seems to be ~3/5 of the proper height, and the UIPopoverController
spans all the way down to the bottom of the screen.
Please see the code below.
Thanks!
self.picker = [[[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)] autorelease];
self.picker.backgroundColor = [UIColor clearColor];
self.picker.showsSelectionIndicator = YES;
self.picker.delegate = self;
self.picker.dataSource = self;
self.picker.transform = CGAffineTransformMakeScale(-1, 1);
UIViewController *pickerController = [[UIViewController alloc] init];
[pickerController setView:self.picker];
UIPopoverController *pickerPopover = [[UIPopoverController alloc] initWithContentViewController:pickerController];
[pickerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
pickerPopover.delegate = self;
self.popover = pickerPopover;
[pickerController release];
[pickerPopover release];
The reason for the "squashed" picker view seems to be this line:
[pickerController setView:self.picker];
Instead, add the picker view as a subview:
[pickerController.view addSubview:picker];
Next, to fix the popover height, set popoverContentSize
before presenting it:
pickerPopover.popoverContentSize = picker.frame.size;
Also, fix the picker view's frame from this:
CGRectMake(0, self.view.bounds.size.height+44, 320, 216)
to this:
CGRectMake(0, 0, 320, 216)
来源:https://stackoverflow.com/questions/8088902/uipopovercontroller-too-large-and-uipickerview-too-small