你不再需要UIPopoverController
用于呈现视图控制器。
相反,您可以设置modalPresentationStyle
视图控制器的UIModalPresentationPopover
.
您可以使用以下代码:
avc.modalPresentationStyle = UIModalPresentationPopover;
avc.popoverPresentationController.sourceView = theButton;
[self presentViewController:avc animated:YES completion:nil];
UIModalPresentationPopover
在水平规则环境中,
内容显示在弹出视图中的演示风格。
背景内容变暗并点击弹出窗口之外的原因
要关闭的弹出窗口。如果您不想点击来关闭
popover,您可以将一个或多个视图分配给 passthroughViews
关联的 UIPopoverPresentationController 对象的属性,
您可以从 popoverPresentationController 属性中获取它。
在水平紧凑的环境中,此选项的行为与
UIModalPresentationFullScreen。
适用于 iOS 8.0 及更高版本。
参考UIModalPresentationStyle 参考 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/c/tdef/UIModalPresentationStyle
您需要设置sourceView
or 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];
Refer 源视图 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPopoverPresentationController_class/index.html#//apple_ref/occ/instp/UIPopoverPresentationController/sourceView and 源矩形 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPopoverPresentationController_class/index.html#//apple_ref/occ/instp/UIPopoverPresentationController/sourceRect更多细节。