ios8中iphone的UIPopoverController显示白屏
Posted
技术标签:
【中文标题】ios8中iphone的UIPopoverController显示白屏【英文标题】:UIPopoverController for iphone in ios8 shows white screen 【发布时间】:2014-10-14 12:15:29 【问题描述】:在 iphone 中使用低于 ios8.0 的 UIPopovercontroller 可以正常使用此代码。但是在ios8中它显示白屏。
代码:
pickerController = [[UIViewController alloc] init];
UIView *viewV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 160)];
[viewV setBackgroundColor:[UIColor clearColor]];
popOverController = [[UIPopoverController alloc] initWithContentViewController:pickerController];
popOverController.popoverContentSize = CGSizeMake(150, 160);
[popOverController setDelegate:self];
CGRect ImageBtnFrame = [self.view convertRect:sender.frame fromView:self.view];
[popOverController presentPopoverFromRect:ImageBtnFrame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
ios8 的任何替代方案,需要建议。
【问题讨论】:
【参考方案1】:根据2014 WWDC,在30分钟内,正确答案是:
- (void) tapButton:(id) sender
MyViewControllerClass * vc = [[MyViewControllerClass alloc] init];
vc.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController * popOverController = vc.popoverPresentationController;
[popOverController setDelegate:self];
popOverController.sourceView = sender;
popOverController.sourceRect = ((UIButton*)sender).frame;
popOverController.permittedArrowDirections = UIPopoverArrowDirectionUp;
[self presentViewController:vc
animated:YES
completion:nil];
请注意,接受的答案存在一些问题,例如:
未设置modalPresentationStyle
显示UIPopoverPresentationController *
而不是UIViewController
【讨论】:
是的,你是对的。这应该是公认的答案。【参考方案2】:尝试对弹出框使用新的 iOS 8 API。
pickerController = [[UIViewController alloc] init];
UIView *viewV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 160)];
[viewV setBackgroundColor:[UIColor clearColor]];
UIPopoverPresentationController *popOverController = pickerController .popoverPresentationController;
popOverController.popoverContentSize = CGSizeMake(150, 160);
[popOverController setDelegate:self];
popOverController.sourceView = self.view;
popOverController.sourceRect = sender.frame;
popOverController.permittedArrowDirections = UIPopoverArrowDirectionUp;
[self presentViewController:popOverController
animated:YES
completion:nil];
【讨论】:
[self presentViewController:popOverController animated:YES completion:nil];
给出警告:Incompatible pointer types sending 'UIPopoverPresentationController *' to parameter of type 'UIViewController * _Nonnull'
【参考方案3】:
这会在 iOS 8 上造成。所以,我建议使用以下 Github 库。 https://github.com/skywinder/ActionSheetPicker-3.0 或者您可以编写基于 iOS 版本的代码。
if (OLDER_THAN_IOS_8)
// Your regular code
pickerController = [[UIViewController alloc] init];
UIView *viewV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 160)];
[viewV setBackgroundColor:[UIColor clearColor]];
popOverController = [[UIPopoverController alloc] initWithContentViewController:pickerController];
popOverController.popoverContentSize = CGSizeMake(150, 160);
[popOverController setDelegate:self];
CGRect ImageBtnFrame = [self.view convertRect:sender.frame fromView:self.view];
[popOverController presentPopoverFromRect:ImageBtnFrame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
else
// New Code that support iOS -8 using UIPopoverPresentationController
// Checkout @AndreasZ answer for the same
【讨论】:
以上是关于ios8中iphone的UIPopoverController显示白屏的主要内容,如果未能解决你的问题,请参考以下文章
iPhone 开发。程序:当 XCode 和 IOS8 公开发布时会发生啥?