From 8e96460ec07ff672175ca128fdf0e925a586c7d6 Mon Sep 17 00:00:00 2001 From: Jimmie Jensen Date: Mon, 10 Apr 2017 09:58:11 +0200 Subject: [PATCH] Gracefully trying to resolve last view controller of current window, otherwise fallback to keyWindow. Also tries to unwrap gracefully instead of forcing it. --- Classes/Popover.swift | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Classes/Popover.swift b/Classes/Popover.swift index d7fbe6c..d4169fd 100644 --- a/Classes/Popover.swift +++ b/Classes/Popover.swift @@ -174,7 +174,10 @@ open class Popover: UIView { } open func showAsDialog(_ contentView: UIView) { - self.showAsDialog(contentView, inView: UIApplication.shared.windows.first!) + guard let rootView = UIApplication.shared.windows.last ?? UIApplication.shared.keyWindow else { + return + } + self.showAsDialog(contentView, inView: rootView) } open func showAsDialog(_ contentView: UIView, inView: UIView) { @@ -185,7 +188,10 @@ open class Popover: UIView { } open func show(_ contentView: UIView, fromView: UIView) { - self.show(contentView, fromView: fromView, inView: UIApplication.shared.windows.first!) + guard let rootView = UIApplication.shared.windows.last ?? UIApplication.shared.keyWindow else { + return + } + self.show(contentView, fromView: fromView, inView: rootView) } open func show(_ contentView: UIView, fromView: UIView, inView: UIView) { @@ -205,7 +211,10 @@ open class Popover: UIView { } open func show(_ contentView: UIView, point: CGPoint) { - self.show(contentView, point: point, inView: UIApplication.shared.windows.first!) + guard let rootView = UIApplication.shared.windows.last ?? UIApplication.shared.keyWindow else { + return + } + self.show(contentView, point: point, inView: rootView) } open func show(_ contentView: UIView, point: CGPoint, inView: UIView) {