From f02439dc0cb8b99234749a3b3bea6c8013ba7820 Mon Sep 17 00:00:00 2001 From: Stephen Sowole Date: Mon, 24 Feb 2020 15:20:14 -0800 Subject: [PATCH] [PanModal] Fix issue with incorrect returned bottom offset value (#84) --- .../PanModalPresentable+LayoutHelpers.swift | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/PanModal/Presentable/PanModalPresentable+LayoutHelpers.swift b/PanModal/Presentable/PanModalPresentable+LayoutHelpers.swift index 5775e0c..ff4805c 100644 --- a/PanModal/Presentable/PanModalPresentable+LayoutHelpers.swift +++ b/PanModal/Presentable/PanModalPresentable+LayoutHelpers.swift @@ -27,10 +27,11 @@ extension PanModalPresentable where Self: UIViewController { Gives us the safe area inset from the top. */ var topLayoutOffset: CGFloat { - guard let application = UIApplication.value(forKeyPath: #keyPath(UIApplication.shared)) as? UIApplication else { - return 0 - } - return application.keyWindow?.rootViewController?.topLayoutGuide.length ?? 0 + + guard let rootVC = rootViewController + else { return 0} + + if #available(iOS 11.0, *) { return rootVC.view.safeAreaInsets.top } else { return rootVC.topLayoutGuide.length } } /** @@ -38,10 +39,11 @@ extension PanModalPresentable where Self: UIViewController { Gives us the safe area inset from the bottom. */ var bottomLayoutOffset: CGFloat { - guard let application = UIApplication.value(forKeyPath: #keyPath(UIApplication.shared)) as? UIApplication else { - return 0 - } - return application.keyWindow?.rootViewController?.bottomLayoutGuide.length ?? 0 + + guard let rootVC = rootViewController + else { return 0} + + if #available(iOS 11.0, *) { return rootVC.view.safeAreaInsets.bottom } else { return rootVC.bottomLayoutGuide.length } } /** @@ -106,5 +108,13 @@ extension PanModalPresentable where Self: UIViewController { } } + private var rootViewController: UIViewController? { + + guard let application = UIApplication.value(forKeyPath: #keyPath(UIApplication.shared)) as? UIApplication + else { return nil } + + return application.keyWindow?.rootViewController + } + } #endif