SwiftUI Add Padding at UITextField for space between keyboard at focus

Viewed 275

At this picture there is a ScrollView with a UIRepresentable TextField inside if the user want to rename an entry. If I add an item the textfield appears when an new item appears. The Scrollview scrolls to the bottom that the textfield is shown.

How can I make that the scrollview scrolls a bit lower so the padding is seen too, without giving the textfield a frame? Is it only possible if I give it a frame, because with a frame the scrollview scrolls to the very bottom? I would like that the scrollview scrolls to the bottom also if I only add a padding to the textfield.

enter image description here

Here is an simple example which is near at the real example:

ScrollView{
    VStack{
        ForEach(model,id: \.self){test in 
            if test.selected{
                UIRepresentableTextField(text: $text)
                     .padding(10)
            }else{
                test.text
                     .padding(10)
            }
        }
    }
}

Here is the makeUiView method in the textfieldrepresentable

func makeUIView(context: UIViewRepresentableContext<CustomTextField>) -> UITextField {
        let textField = UITextField(frame: .zero)
        textField.returnKeyType = .done
        textField.delegate = context.coordinator
        textField.keyboardType = .namePhonePad
        textField.autocorrectionType = suggestions ? .yes : .no
        textField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        return textField
    }
0 Answers
Related