Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to preserve status bar space #488

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions SwiftMessages/SwiftMessages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions SwiftMessages/WindowViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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
}
Expand Down