Jetpack Compose Side Effect to update Android or iOS status-bar and navigation-bar color
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
actual fun PlatformColors(statusBarColor: Color, navBarColor: Color){ | |
val sysUiController = rememberSystemUiController() | |
SideEffect { | |
sysUiController.setSystemBarsColor(color = topColor) | |
sysUiController.setNavigationBarColor(color = bottomColor) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
expect fun PlatformColors(statusBarColor: Color, navBarColor: Color) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
private fun statusBarView() = remember { | |
val keyWindow: UIWindow? = | |
UIApplication.sharedApplication.windows.firstOrNull { (it as? UIWindow)?.isKeyWindow() == true } as? UIWindow | |
val tag = 3848245L // https://stackoverflow.com/questions/56651245/how-to-change-the-status-bar-background-color-and-text-color-on-ios-13 | |
keyWindow?.viewWithTag(tag)?.let { | |
it | |
} ?: run { | |
val height = | |
keyWindow?.windowScene?.statusBarManager?.statusBarFrame ?: zeroValue<CGRect>() | |
val statusBarView = UIView(frame = height) | |
statusBarView.tag = tag | |
statusBarView.layer.zPosition = 999999.0 | |
keyWindow?.addSubview(statusBarView) | |
statusBarView | |
} | |
} | |
@Composable | |
actual fun PlatformColors( | |
statusBarColor: Color, | |
navBarColor: Color | |
) { | |
val statusBar = statusBarView() | |
SideEffect { | |
statusBar?.backgroundColor = topColor.toUIColor() | |
UINavigationBar.appearance().backgroundColor = bottomColor.toUIColor() | |
} | |
} | |
private fun Color.toUIColor(): UIColor = UIColor( | |
red = this.red.toDouble(), | |
green = this.green.toDouble(), | |
blue = this.blue.toDouble(), | |
alpha = this.alpha.toDouble() | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment