https://kotlinlang.org logo
Title
a

Anmol Verma

05/04/2023, 3:32 PM
Has anyone tried to use iOS native UITextField since compose textfield has some weird issues for now ? I came up with something like this, wanted to check if someone already went ahead this path and had any issues ?
@Composable
actual fun TextField(
    modifier: Modifier,
    text: String,
    placeholder: String?,
    onTextChange: (String) -> Unit
) {
    UIKitView(
        modifier = modifier,
        factory = {
            val label = UITextField()
            label.setPlaceholder(placeholder)
            label.setText(text)
            label
        }, onResize = { view: UIView, rect: CValue<CGRect> ->
            CATransaction.begin()
            CATransaction.setValue(true, kCATransactionDisableActions)
            view.layer.setFrame(rect)
            CATransaction.commit()
        }, update = {
            onTextChange(it.text() ?: "")
        })
}
a

Alexander Zhirkevich

05/04/2023, 8:57 PM
Had this issue https://github.com/JetBrains/compose-multiplatform/issues/3119. Btw, basic text field is already implemented as ComposeUITextField.
s

Shivam Kanodia

05/05/2023, 9:42 AM
how can i use this in shared code i.e commonMain?
a

Anmol Verma

05/05/2023, 9:59 AM
You will have to create a expected fun and import
<https://github.com/JetBrains/compose-multiplatform-core/blob/f9dfd04e830b08d6e81463daa78551b0b8c28e96/compose/ui/ui/src/uikitMain/kotlin/androidx/compose/ui/interop/ComposeUITextField.uikit.kt#L19|ComposeUITextField>
in iosMain sources @Shivam Kanodia
a

Animasahun Ibrahim

05/05/2023, 9:00 PM
@Shivam Kanodia @Anmol Verma
@Composable
expect fun PlatformTextField(value: String, onValueChange: (String) -> Unit, modifier: Modifier)
iOS
@Composable
actual fun PlatformTextField(
    value: String,
    onValueChange: (String) -> Unit,
    modifier: Modifier,
) = ComposeUITextField(value, onValueChange, modifier)
Android
@Composable
actual fun PlatformTextField(
    value: String,
    onValueChange: (String) -> Unit,
    modifier: Modifier,
) = TextField(value, onValueChange, modifier)