简体   繁体   中英

SystemUIController won't set status bar color - Jetpack Compose Accompanist

I'm not sure when or what I changed, but all of the sudden the systemUiController has stopped affecting the status bar color in my app. For context, I'm using the accompanist Insets library in combination with system UI controller to get rid of the status and nav bar, and this was working fine until a build or two ago, and now its only working on the nav/gesture bar.

I do this by putting all composables into ProvideWindowInsets, and by setting the bar colors to transparent. For some reason this only works on the nav bar.

To debug, I've stripped my app of everything but the setContent in onCreate of the mainActivity, including all services, and have temporarily removed everything within setContent but an empty theme provider.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    WindowCompat.setDecorFitsSystemWindows(window, false)

    setContent {
            val systemUiController = rememberSystemUiController()
            val useDarkIcons = !isSystemInDarkTheme()
            SideEffect {
                systemUiController.setSystemBarsColor(
                    color = Color.Transparent,
                    darkIcons = useDarkIcons
                )
            }
            MicCheckTheme {
                ProvideWindowInsets() {
                    
                }
            }
    }
}

When I run this, only the nav bar is transparent. I believe this isn't the fault of the Insets library, as while the status bar retains its color, app content goes underneath the bar, so Insets works as intended.

For further context, I'm using a MainActivity inheriting ComponentActivity with Material3, Compose 1.2.x, and accompanist 0.24.9-beta, although I've tried many different releases in debugging this, none working. I've even created a new test app project and pasted the Insets + SystemUIController sample app from the accompanist github, and even that didn't work.

Please help me. Thanks.

That is because in Material3 compose Theme.kt overrides systemuicontroller changes as follows:

 val view = LocalView.current
if (!view.isInEditMode) {
    SideEffect {
        (view.context as Activity).window.statusBarColor = colorScheme.primary.toArgb()
        ViewCompat.getWindowInsetsController(view)?.isAppearanceLightStatusBars = darkTheme
    }
}

As a matter of fact, you do not need systemuicontroller libary to update system bars

. You can directly utilise the above code from Theme.kt in Material3 Compose template.

Note: ViewCompat.getWindowInsetsController(view) is now depricated. You should use WindowCompat.getInsetsController(window, view)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM