Is there any solution available for the textfield ...
# compose-ios
a
Is there any solution available for the textfield on iOS ? The compose version doesn’t work the way I want 🤦‍♂️
also ComposeUITextField runs out of the screen at times
j
What doesnt work? :) Both works good imo. If not happy can always do a expect/actual backward interop with uikit or SwiftUi.
I am trying to use
Copy code
modifier.onGloballyPositioned { it ->
            sizeText = it.size
        }
to solve this, let's see 🩺
j
Feels like its need rememberupdated for factory if refers to other mutable states. Also in sample 3 fields all refer to same text, could trigger each other recompose.
a
I tried to remember {} the factory lambda and it does seem to work 🤞
j
Curious, how does factory lambda looks like?
a
Copy code
val factory = remember {
        val textField = object : UITextField(CGRectMake(0.0, 0.0, 0.0,0.0)) {
            @ObjCAction
            fun editingChanged() {
                onValueChange(text ?: "")
            }
        }
        textField.placeholder = (label)
        textField.setBorderStyle(UITextBorderStyle.UITextBorderStyleRoundedRect)
        textField.secureTextEntry = passwordField
        textField.addTarget(
            target = textField,
            action = NSSelectorFromString(textField::editingChanged.name),
            forControlEvents = UIControlEventEditingChanged
        )
        textField
    }

    UIKitView(
        factory = {
            factory
        },
j
Interesting. Regular in compose world want factory recreated when using equivalent of AndroidView interop (as state kept outside and just refresh ui). I guess SwiftUi works different. Also I think want rememberUpdated to make sure lambda updates text while typing but uncertain.
Btw have you tested using the compose BasicTextField and/or OutlinedTextField from compose material?
In your sample following works for me:
Copy code
OutlinedTextField(
                value = text1,
                onValueChange = {
                    text1 = it
                    someRememberedObject.mutate()
                },
                modifier = Modifier.width(200.dp).height(50.dp)
            )
I could reproduce the issue first. Quite interesting only happening in ComposeUITextField but would think bridge between compose and Swift not properly setup for factory stuff.
a
Just remember factory lambda if your uikit view dissapears. For ComposeUITextField you may try to remember onValueChange lambda, but i doubt it will work. Need to rewrite ComposeUITextField with factory remembering or wait for a fix.
@Elijah Semyonov How are things going with UIKitView fix? Seems like more and more people start facing this issue
@Anmol Verma In your code example you remembered just a UITextField. You need to remember factory of UITextField. Like this (2 pairs of brackets):
Copy code
val factory = remember {
    { UITextField()  }
} 

UIKitView(
    factory = factory
)
a
Yes I tired that, but I am falling back to SwiftUI for this screen which needs forms, because even if i get it working, the scrolling is messed up with all those text fields
a
Yeah srcolling (especially lazy) works so bad with UIKitViews. They can't keep up with the scroll
e
@Alexander Zhirkevich gotta roll PR for it now. Didn’t look into scrolling behavior yet, thanks for mentioning it.
a
It is not that bad tho :). Best seen on complex screens. And may be in release mode it works better.