UIPopoverController too large and UIPickerView too small

余生颓废 提交于 2019-12-01 23:50:07

问题


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:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!