【发布时间】:2017-07-24 16:08:38
【问题描述】:
我正在尝试定义一个附加到这样的视图的弹出视图:
这是我的代码:
class MyController: UIViewController, UIPopoverPresentationControllerDelegate {
...
func displaySignOut(_ sender: UIButton) {
let vc = UIStoryboard(name: "Main", bundle: nil)
.instantiateViewController(withIdentifier: "signOutPopover")
vc.modalPresentationStyle = .popover
vc.preferredContentSize = CGSize(width: 100, height: 30)
present(vc, animated: true, completion: nil)
let pc = vc.popoverPresentationController!
pc.sourceView = sender
pc.sourceRect = sender.bounds
pc.delegate = self
}
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return .none
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
}
因为弹出框很小,我想在所有设备上都使用这种样式。我遵循了关于覆盖adaptivePresentationStyle 以返回UIModalPresentationStyle.none 的常规建议(例如here)。
这在 iPad 设备上运行良好,但在 iPhone 上却不行。在较小的 iPhone 设备上,它总是全屏显示。在较大的屏幕(例如,iPhone 7 Plus)上,它会出现错误,但奇怪的是,如果我在弹出框出现后旋转设备,则会切换到弹出框演示(纵向和横向)。 (如果我关闭弹出框并再次打开它,在我旋转设备之前它又是错误的。)此外,在横向它会以一种奇怪的配置出现(不像纵向那样全屏):
与弹出式演示不同,如果我在弹出式视图本身之外点击,它不会关闭。
Apple documentation 说(部分):
在水平紧凑的环境中,弹出框默认适应
UIModalPresentationOverFullScreen呈现风格。
“默认”强烈暗示有一种方法可以覆盖此行为。但是(与this post 一致),在委托中覆盖adaptivePresentationStyle 似乎不再是这样做的方法(尽管它曾经有效)。那么有没有新的方法来修改默认行为呢?
我正在使用 XCode 8.3.3 和 Swift 3.1,针对 iOS 9+。
【问题讨论】: