diff --git a/SwiftMessages/SwiftMessages.swift b/SwiftMessages/SwiftMessages.swift index 5e9f0c3..b4b56c6 100644 --- a/SwiftMessages/SwiftMessages.swift +++ b/SwiftMessages/SwiftMessages.swift @@ -311,6 +311,14 @@ open class SwiftMessages { */ public var prefersStatusBarHidden: Bool? + /** + Specifies whether or not the space used by the status bar should be preserved + after hiding the status bar for devices without a notch. This can be useful in + combination with `prefersStatusBarHidden` to show the view on the space + used by the status bar. The default is `false` + */ + public var preserveStatusBarSpace: Bool = false + /** If a view controller is created to host the message view, should the view controller auto rotate? The default is 'true', meaning it should auto diff --git a/SwiftMessages/WindowViewController.swift b/SwiftMessages/WindowViewController.swift index 9c2b721..a03a8c3 100644 --- a/SwiftMessages/WindowViewController.swift +++ b/SwiftMessages/WindowViewController.swift @@ -49,6 +49,15 @@ open class WindowViewController: UIViewController private func show(becomeKey: Bool, frame: CGRect? = nil) { guard let window = window else { return } + if #available(iOS 13.0, *), + config.preserveStatusBarSpace, + let previousKeyWindowSafeAreaInsets = previousKeyWindow?.safeAreaInsets, + window.safeAreaInsets.top != previousKeyWindowSafeAreaInsets.top + { + initialAdditionalSafeAreaInsets = previousKeyWindow?.rootViewController?.additionalSafeAreaInsets + previousKeyWindow?.rootViewController?.additionalSafeAreaInsets.top += previousKeyWindowSafeAreaInsets.top + } + window.frame = frame ?? UIScreen.main.bounds if becomeKey { window.makeKeyAndVisible() @@ -63,6 +72,9 @@ open class WindowViewController: UIViewController } if #available(iOS 13, *) { window?.windowScene = nil + if let initialAdditionalSafeAreaInsets = initialAdditionalSafeAreaInsets { + previousKeyWindow?.rootViewController?.additionalSafeAreaInsets = initialAdditionalSafeAreaInsets + } } window?.isHidden = true window = nil @@ -84,6 +96,7 @@ open class WindowViewController: UIViewController private var window: UIWindow? private weak var previousKeyWindow: UIWindow? + private var initialAdditionalSafeAreaInsets: UIEdgeInsets? let config: SwiftMessages.Config }