Saturday, November 7, 2026
HomeiOS Developmentios - Issues concerning UI textual content discipline instantiation with swift UI

ios – Issues concerning UI textual content discipline instantiation with swift UI

[ad_1]

I would like so as to add further performance to the swiftui TextField, and for that I added the next wrapper to my textual content fields:

struct TextFieldDynamicWidth: View {
    let title: String
    @Binding var textual content: String
    @Binding var isFirstResponder: Bool
    let onEditingChanged: (Bool) -> Void
    let onCommit: () -> Void

    @State non-public var textRect = CGRect()

    var physique: some View {
        ZStack {
            Textual content(textual content.isEmpty ? title : textual content).background(GlobalGeometryGetter(rect: $textRect)).layoutPriority(1).opacity(0)
            HStack {
                CustomOnbordTextField(textual content: $textual content, isFirstResponder: $isFirstResponder, textRect: $textRect)
                    .body(width: textRect.width)
            }
        }
    }
}

struct CustomOnbordTextField: UIViewRepresentable {
    @Binding public var isFirstResponder: Bool
    @Binding public var textual content: String
    @Binding public var textRect: CGRect
    non-public var tempTextField = UITextField()

    non-public var configuration = { (_: UITextField) in }

    public init(textual content: Binding<String>, isFirstResponder: Binding<Bool>, textRect: Binding<CGRect>, configuration: @escaping (UITextField) -> Void = { _ in }) {
        self.configuration = configuration
        self._text = textual content
        self._isFirstResponder = isFirstResponder
        self._textRect = textRect
    }

    func makeUIView(context: Context) -> UITextField {
        let view = tempTextField
        view.placeholder = "0"
        view.font = UIFont(identify: "EuclidCircularB-Common", dimension: 70)
        view.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        view.addTarget(context.coordinator, motion: #selector(Coordinator.textViewDidChange), for: .editingChanged)
        view.delegate = context.coordinator
        view.keyboardType = .numberPad
        view.body.dimension.width = textRect.width
        return view
    }

    func updateUIView(_ uiView: UITextField, context: Context) {
        uiView.textual content = textual content
        configuration(uiView)
        print("##### isFirstResponder -> (isFirstResponder)")
        if isFirstResponder {
            uiView.becomeFirstResponder()
        }
    }

    public func makeCoordinator() -> Coordinator {
        Coordinator($textual content, isFirstResponder: $isFirstResponder)
    }

    public class Coordinator: NSObject, UITextFieldDelegate {
        var textual content: Binding<String>
        var isFirstResponder: Binding<Bool>

        init(_ textual content: Binding<String>, isFirstResponder: Binding<Bool>) {
            self.textual content = textual content
            self.isFirstResponder = isFirstResponder
        }

        @objc public func textViewDidChange(_ textField: UITextField) {
            print("##### textViewDidChange -> (textField.textual content ?? "")")
            self.textual content.wrappedValue = textField.textual content ?? ""
        }

        public func textFieldDidBeginEditing(_ textField: UITextField) {
            print("##### textFieldDidBeginEditing")
            self.isFirstResponder.wrappedValue = true
        }

        public func textFieldDidEndEditing(_ textField: UITextField) {
            print("##### textFieldDidEndEditing")
            self.isFirstResponder.wrappedValue = false
        }

        public func textField(_ textField: UITextField, shouldChangeCharactersIn vary: NSRange, replacementString string: String) -> Bool {
            return true
        }
    }
}

The issue is that once I navigate to a special view and use this very same code it doesn’t create new cases strategies of the textfield, even when I duplicate the code and alter the identify of those two structs to one thing like (TextFieldDynamicWidth111) and (CustomOnbordTextField111) the strategy updateUIView(_ uiView: UITextField, context: Context) stops responding…principally how do I make distinctive cases of this code or deinit the earlier view in order that the delegate methodology can work?

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments