UIPopoverController too large and UIPickerView too

2019-02-24 12:23发布

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

1条回答
Juvenile、少年°
2楼-- · 2019-02-24 13:14

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)
查看更多
登录 后发表回答