如何修复“UIPopoverController已弃用”?警告? [英] How can I fix the "UIPopoverController is deprecated" warning?
问题描述
我正在使用此代码:
mediaLibraryPopover = [[UIPopoverController alloc]
initWithContentViewController:avc];
[self.mediaLibraryPopover presentPopoverFromRect:[theButton bounds]
inView:theButton
allowedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
我在Xcode 7中收到此警告:
不推荐使用UIPopoverController,在iOS 9.0中首先弃用 - 不推荐使用UIPopoverController。弹出窗口现在实现为UIViewController演示。使用UIModalPresentationPopover和UIPopoverPresentationController的模态演示风格。
您不再需要 UIPopoverController
用于呈现视图控制器。
相反,您可以将视图控制器的 modalPresentationStyle
设置为 UIModalPresentationPopover
。
您可以使用以下代码:
avc.modalPresentationStyle = UIModalPresentationPopover;
avc.popoverPresentationController.sourceView = theButton;
[self presentViewController:avc animated:YES completion:nil];
UIModalPresentationPopover
在水平常规环境中,
表示样式,其中内容显示在弹出视图中。
背景内容变暗,弹出窗口外的水龙头导致
弹出窗口被解雇。如果你不想点击消除
popover,你可以将一个或多个视图分配给相关UIPopoverPresentationController对象的passthroughViews
属性,
你可以从popoverPresentationController属性获得。 / p>
在水平紧凑的环境中,此选项与
UIModalPresentationFullScreen的行为相同。
在iOS中可用8.0及更高版本。
参考 UIModalPresentationStyle Reference
你需要设置 sourceView
或 barButtonItem
属性,否则它将崩溃并显示以下消息:
***由于未捕获的异常'NSGenericException'而终止应用程序,原因:'UIPopoverPresentationController (***)在演示发生之前应该设置一个非零
的sourceView或barButtonItem。'
用于锚定正确的弹出箭头,您还需要指定 sourceRect
属性。
avc.modalPresentationStyle = UIModalPresentationPopover;
avc.popoverPresentationController.sourceView = self.view;
avc.popoverPresentationController.sourceRect = theButton.frame;
[self presentViewController:avc animated:YES completion:nil];
参考sourceView 和sourceRect 了解更多详情。
I am using this code:
mediaLibraryPopover = [[UIPopoverController alloc]
initWithContentViewController:avc];
[self.mediaLibraryPopover presentPopoverFromRect:[theButton bounds]
inView:theButton
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
And I am getting this warning in Xcode 7:
UIPopoverController is deprecated, first deprecated in iOS 9.0 - UIPopoverController is deprecated. Popovers are now implemented as UIViewController presentations. Use a modal presentation style of UIModalPresentationPopover and UIPopoverPresentationController.
You no longer need UIPopoverController
for presenting a view controller.
Instead you can set the modalPresentationStyle
of view controller to UIModalPresentationPopover
.
You can use the following code for that:
avc.modalPresentationStyle = UIModalPresentationPopover;
avc.popoverPresentationController.sourceView = theButton;
[self presentViewController:avc animated:YES completion:nil];
UIModalPresentationPopover
In a horizontally regular environment, a presentation style where the content is displayed in a popover view. The background content is dimmed and taps outside the popover cause the popover to be dismissed. If you do not want taps to dismiss the popover, you can assign one or more views to the passthroughViews property of the associated UIPopoverPresentationController object, which you can get from the popoverPresentationController property.
In a horizontally compact environment, this option behaves the same as UIModalPresentationFullScreen.
Available in iOS 8.0 and later.
Reference UIModalPresentationStyle Reference
You need to set either sourceView
or barButtonItem
property, else it will crash with the following message:
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (***) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'
For anchoring the popover arrow correctly, you need to specify the sourceRect
property also.
avc.modalPresentationStyle = UIModalPresentationPopover;
avc.popoverPresentationController.sourceView = self.view;
avc.popoverPresentationController.sourceRect = theButton.frame;
[self presentViewController:avc animated:YES completion:nil];
Refer sourceView and sourceRect for more details.
这篇关于如何修复“UIPopoverController已弃用”?警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!