在 IOS 8 中,这样设置是没有问题的,但是在 IOS 7 中,这样的设置就会出现问题了,无论你怎样设置,popover 出来的内容框大小依然是默认的大小。
_popoverController = [[UIPopoverController alloc] initWithContentViewController:_contentViewController];
_popoverController.delegate = self;
_contentViewController.preferredContentSize = _contentViewController.view.bounds.size;
[_popoverController presentPopoverFromRect:position inView:view permittedArrowDirections:UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown animated:YES];
查了一下资料,在 IOS 7 中需要把
_contentViewController.preferredContentSize = _contentViewController.view.bounds.size;
提前到
_popoverController = [[UIPopoverController alloc] initWithContentViewController:_contentViewController];
之前。这样,设置 popover 的大小就能成功生效。
tips:
1、记得不要把 _popoverController 和 _contentViewController 设置为局部变量,不然在 popover 完了之后这两个变量就被回收了,在进行其他操作的话马上就会出现 crash 了。
关于 popoverContentSize 大小的问题,stackoverflow 的参考如下:
Take two steps to solve this
Step One:
As an immediate fix, you can set preferredContentSize
property of UIViewController
[popoverViewController setPreferredContentSize:CGSizeMake(248.0,216.0)];
before initalizing UIPopoverController
UIPopoverController *popoverController = [[UIPopoverController alloc]initWithContentViewController:popoverViewController];
Step Two:
In iOS8, UIPopoverPresentationController
is getting introduced as a replacement of UIPopoverController
if ([popoverViewController respondsToSelector:@selector(popoverPresentationController)]) { popoverViewController.modalPresentationStyle = UIModalPresentationPopover; [popoverViewController setPreferredContentSize:CGSizeMake(248.0,216.0)]; UIPopoverPresentationController *popoverPresentation = popoverViewController.popoverPresentationController; [popoverPresentation setSourceView:_sourceView]; [popoverPresentation setSourceRect:_sourceRect]; [popoverPresentation setPermittedArrowDirections:UIPopoverArrowDirectionUp]; [self presentViewController:popoverViewController animated:YES completion:nil]; } else { //existing code... }
As of now, pre-release documentation is available for further reference.
参考链接:http://stackoverflow.com/questions/24772933/popovercontentsize-not-responding-in-ios-8