iOS 8 presentationController确定是否真的是popover [英] iOS 8 presentationController determine if really is popover
问题描述
我正在使用iOS 8的新自适应Present As Popover功能。我在StoryBoard中连接了一个简单的segue来进行演示。它在iPhone 6 Plus上运行良好,因为它将视图显示为弹出窗口,在iPhone 4s上显示为全屏视图(工作表样式)。
I'm using the new adaptive "Present As Popover" capability of iOS 8. I wired up a simple segue in the StoryBoard to do the presentation. It works great on an iPhone 6 Plus as it presents the view as a popover and on an iPhone 4s it shows as a full screen view (sheet style).
问题当显示为全屏视图时,我需要在视图中添加完成按钮,以便可以调用dismissViewControllerAnimated。当它显示为弹出窗口时,我不想显示完成按钮。
The problem is when shown as a full screen view, I need to add a "Done" button to the view so dismissViewControllerAnimated can be called. And I don't want to show the "done" button when it's shown as a popover.
我试着查看presentationController和popoverPresentationController的属性,我什么都没找到,告诉我如果它实际上显示为弹出框。
I tried looking at the properties of both presentationController and popoverPresentationController, and I can find nothing that tells me if it is actually being shown as a popover.
NSLog( @"View loaded %lx", (long)self.presentationController.adaptivePresentationStyle ); // UIModalPresentationFullScreen
NSLog( @"View loaded %lx", (long)self.presentationController.presentationStyle ); // UIModalPresentationPopover
NSLog( @"View loaded %lx", (long)self.popoverPresentationController.adaptivePresentationStyle ); // UIModalPresentationFullScreen
NSLog( @"View loaded %lx", (long)self.popoverPresentationController.presentationStyle ); // UIModalPresentationPopover
adaptivePresentationStyle总是返回UIModalPresentationFullScreen,而presentationStyle总是返回UIModalPresentationPopover
adaptivePresentationStyle always returns UIModalPresentationFullScreen and presentationStyle always returns UIModalPresentationPopover
在查看UITraitCollection时,我发现了一个名为_UITraitNameInteractionModel的特征,当它实际显示为Popover时,它只被设置为1。但是,Apple不提供通过popoverPresentationController的traitCollection直接访问该特征。
When looking at the UITraitCollection I did find a trait called "_UITraitNameInteractionModel" which was only set to 1 when it was actually displayed as a Popover. However, Apple doesn't provide direct access to that trait through the traitCollection of popoverPresentationController.
推荐答案
最好的方式(最不臭)我发现这样做是为了使用 UIPopoverPresentationControllerDelegate
。
The best way (least smelly) I've found to do this is to use the UIPopoverPresentationControllerDelegate
.
•确保在 UIPopoverPresentationController上将呈现的视图控制器设置为
用于管理演示文稿。我正在使用故事板,所以在 UIPopoverPresentationControllerDelegate
prepareForSegue中设置:
segue.destinationViewController.popoverPresentationController.delegate = presentedVC;
•在呈现的视图控制器中创建一个属性以跟踪此状态:
• Create a property in the presented view controller to keep track of this state:
@property (nonatomic, assign) BOOL amDisplayedInAPopover;
•添加以下委托方法(或添加到现有的委托方法):
• And add the following delegate method (or add to your existing delegate method):
- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController
{
// This method is only called if we are presented in a popover
self.amDisplayedInAPopover = YES;
}
•最后在 viewWillAppear:
- viewDidLoad:
太早,在 viewDidLoad:
和<$ c之间调用委托prepare方法$ c> viewWillAppear:
• And then finally in viewWillAppear:
- viewDidLoad:
is too early, the delegate prepare method is called between viewDidLoad:
and viewWillAppear:
if (self.amDisplayedInAPopover) {
// Hide the offending buttons in whatever manner you do so
self.navigationItem.leftBarButtonItem = nil;
}
编辑:更简单的方法!
只需设置代理(确保您的presentVC采用 UIPopoverPresentationControllerDelegate
):
Just set the delegate (making sure your presentedVC adopts the UIPopoverPresentationControllerDelegate
):
segue.destinationViewController.popoverPresentationController.delegate = presentedVC;
并提供方法:
- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController
{
// This method is only called if we are presented in a popover
// Hide the offending buttons in whatever manner you do so
self.navigationItem.leftBarButtonItem = nil;
}
这篇关于iOS 8 presentationController确定是否真的是popover的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!