Skip to content

Introspecting TextFields or TextViews to set the inputView doesn't work within conditional Views #436

Open
@paescebu

Description

@paescebu

Description

Hi,

it seems there's a nasty edge case when using introspection on TextFields or TextViews within conditionals:

import SwiftUI
import SwiftUIIntrospect

struct ContentView: View {
    @State private var isShown: Bool = false
    @FocusState private var textFieldFocused
    @State private var textFieldContent = ""

    var body: some View {
        Form {
            Toggle("Show Keyboard", isOn: $isShown)
            if isShown {
                TextField("title", text: $textFieldContent, axis: .vertical)
                    .introspect(.textEditor, on: .iOS(.v18), customize: { textView in
                        let view = UIView()
                        view.backgroundColor = .red
                        view.frame = .init(origin: .zero, size: .init(width: 200, height: 100))
                        textView.backgroundColor = .red
                        textView.inputView = view
                    })
                    .onAppear {
                        textFieldFocused = true
                    }
                    .focused($textFieldFocused)
            }
        }
    }
}

The code above shows that the introspection sets the background color of the textView nicely, but the inputView only receives the right inputView after toggling the flag a second time to true.

Do you have any idea why that could be happening and whether this is solveable?

Kind regards!

Checklist

Expected behavior

No response

Actual behavior

No response

Steps to reproduce

No response

Version information

1.3.0

Destination operating system

iOS18

Xcode version information

Xcode 16.0

Swift Compiler version information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions