Hi everyone, I'm experimenting the onLayoutRectCha...
# compose-ios
e
Hi everyone, I'm experimenting the onLayoutRectChanged API and extract
calculateOcclusions
information. It seems to work well on Android but iOS always return an empty list. Is there any information about this? Thanks a lot.
h
i think if u share code snippets people can assist u easily, you might be doing it in the wrong phase of the composable
e
Sure. I use the official wizard to create a project: https://kmp.jetbrains.com/?android=true&ios=true&iosui=compose&includeTests=true And only update App() to the below:
Copy code
@Composable
@Preview
fun App() {
    MaterialTheme {
        var showContent by remember { mutableStateOf(false) }
        Column(
            modifier = Modifier
                .background(MaterialTheme.colorScheme.primaryContainer)
                .safeContentPadding()
                .fillMaxSize(),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            Button(onClick = { showContent = !showContent }) {
                Text("Click me!")
            }

            AnimatedVisibility(showContent) {
                var occlusions by remember { mutableStateOf<List<IntRect>>(emptyList()) }
                var bounds by remember { mutableStateOf<IntRect?>(null) }

                Box(
                    modifier = Modifier.fillMaxSize()
                ) {
                    Column(
                        modifier = Modifier.fillMaxWidth().align(Alignment.Center)
                            .onLayoutRectChanged(0, 0) {
                                occlusions = it.calculateOcclusions()
                                bounds = it.boundsInScreen
                                println(
                                    """
                                        ---
                                        ${it.calculateOcclusions()},
                                        ${it.boundsInScreen},
                                        ${it.width} x ${it.height}
                                        ${it.fractionVisibleInWindow()}
                                        ---
                                    """.trimIndent()
                                )
                            },
                        horizontalAlignment = Alignment.CenterHorizontally,
                    ) {
                        Image(painterResource(Res.drawable.compose_multiplatform), null)
                        Text("Occlusions: $occlusions\nBounds: $bounds")
                    }

                    Box(
                        modifier = Modifier.size(64.dp).background(Color.Red).align(Alignment.Center),
                    )
                }
            }
        }
    }
}
I place a Red box above the default view which should report at least an occlusion element if it works as expected. I attach the screen record to demonstrate it.