Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions status-bar/ios/Sources/StatusBarPlugin/StatusBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class StatusBar {
private var backgroundColor = UIColor.black
private var backgroundView: UIView?
private var observers: [NSObjectProtocol] = []
private var webViewFrameObservations: [NSKeyValueObservation] = []
private var isApplyingLayout = false

init(bridge: CAPBridgeProtocol, config: StatusBarConfig) {
self.bridge = bridge
Expand All @@ -19,6 +21,7 @@ public class StatusBar {

deinit {
observers.forEach { NotificationCenter.default.removeObserver($0) }
stopObservingWebViewFrame()
}

private func setupObservers(with config: StatusBarConfig) {
Expand All @@ -37,6 +40,42 @@ public class StatusBar {
setStyle(config.style)
setBackgroundColor(config.backgroundColor)
setOverlaysWebView(config.overlaysWebView)
// Fallback for a frame reset that the observer below could not correct.
restoreLayout()
}

// The WebView is the root view of CAPBridgeViewController. When a full-screen
// modal (for example the SFSafariViewController of @capacitor/browser) is
// dismissed, UIKit resets the root view to the full window bounds. Watch the
// frame and move the WebView back below the status bar in the same layout pass.
private func observeWebViewFrame() {
guard webViewFrameObservations.isEmpty, let layer = bridge?.webView?.layer else { return }
webViewFrameObservations = [
layer.observe(\.position) { [weak self] _, _ in self?.webViewFrameDidChange() },
layer.observe(\.bounds) { [weak self] _, _ in self?.webViewFrameDidChange() }
]
}

private func stopObservingWebViewFrame() {
webViewFrameObservations.forEach { $0.invalidate() }
webViewFrameObservations = []
}

private func webViewFrameDidChange() {
guard !isApplyingLayout, !isOverlayingWebview, let webView = bridge?.webView else { return }
let statusBarHeight = getStatusBarFrame().size.height
// Other frame changes, such as the keyboard plugin changing the height, keep the origin.
guard statusBarHeight > 0, webView.frame.origin.y != statusBarHeight else { return }
restoreLayout()
}

private func restoreLayout() {
if !isOverlayingWebview, let backgroundView {
// addSubview also brings the view to the front if it is already a subview.
bridge?.webView?.superview?.addSubview(backgroundView)
}
resizeStatusBarBackgroundView()
resizeWebView()
}

private func handleViewWillTransition() {
Expand Down Expand Up @@ -118,10 +157,12 @@ public class StatusBar {
if overlay == isOverlayingWebview { return }
isOverlayingWebview = overlay
if overlay {
stopObservingWebViewFrame()
backgroundView?.removeFromSuperview()
} else {
initializeBackgroundViewIfNeeded()
bridge?.webView?.superview?.addSubview(backgroundView!)
observeWebViewFrame()
}
resizeWebView()
}
Expand All @@ -133,6 +174,8 @@ public class StatusBar {
let webView = bridge?.webView,
let bounds = bounds
else { return }
isApplyingLayout = true
defer { isApplyingLayout = false }
bridge?.viewController?.view.frame = bounds
webView.frame = bounds
let statusBarHeight = getStatusBarFrame().size.height
Expand Down