9

Cannot draw behind Top and Bottom Bars in IOS.

Hi, I am trying KMP for android and ios but had a problem. In Kotlin blog it says "Using the WindowInsets API, you can draw background content via Compose Multiplatform behind the notch" but I couldn't do it. Does anyone know how can I draw background for Top and Bottom Bar?

1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Feb 21, 2024 at 7:25

1 Answer 1

29

First, disable platform-specific default insets being applied in the app.

  • For IOS you need to disable safe area paddings being applied by the Switft UI by adding the following line in iosApp/iosApp/ContentView.swift file.
// iosApp/iosApp/ContentView.swift

struct ContentView: View {
    var body: some View {
        ComposeView()
                .ignoresSafeArea(edges: .all) // Add this line
                .ignoresSafeArea(.keyboard)
    }
}
  • For Android Make sure to call enableEdgeToEdge() in your Activity's onCreate.
// MainActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    enableEdgeToEdge() // Add this
    setContent {
        // composables
    }
}

Your app will now start drawing content behind the top and bottom bar; Now you can use WindowInsets API from Compose to apply correct padding from within compose.

@Composable
MyScreen() {
    Box(
        Modifier
            .background(Color.Red)
            .windowInsetsPadding(WindowInsets.safeDrawing)
    ) {
         // Content goes here
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm able to change the status bar and nav bar color for the screens that are outside of Scaffold. For the screens that are inside the scaffold, I cannot change color based on the current screen. How can I do that? I want to change the status bar and bottom nav bar color based on the current screen. My screens have different color for status bar and different color for bottom nav bar. By bottom nav bar I mean the system navigation bar.
This works great for drawing behind system bars, but AFAICT it doesn't add the required status/navigation bar insets s.t. you end up with scaffold behind the status bar, at least on iPhone 14.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.