Hi, cederachBiB,
Did a small change to your code that I'd like to share back. I was having problems with TextFieldWithFocus filling up too much space when I wanted a single-line TextField similar to SwiftUI's.
I tried adding a .frame(height: ) to my TextFieldWithFocus element, but it crashed the app. So I added this code to the end of the properties and beginning of the makeUIView(context:)
var enablesReturnKeyAutomatically: Bool = false
@State var frameRect: CGRect = .zero
var onCommit: (() -> Void)?
func setFrameRect(_ rect: CGRect) -> some View {
self.frameRect = rect
return self
}
func makeUIView(context: UIViewRepresentableContext) -> UITextField {
let textField = UITextField(frame: self.frameRect)
textField.delegate = context.coordinator
textField.placeholder = NSLocalizedString(placeholder, comment: "")
And by using a GeometryReader around the element (or elements in a HStack), it's now possbile to set the frame this way, for example:
GeometryReader { geometry in
HStack {
TextFieldWithFocus(text: self.$newString, placeholder: "placeholder", isFirstResponder: self.$textHasFocus,
onCommit: { UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil,
from: nil, for: nil)
self.textHasFocus = false
})
.setFrameRect(geometry.frame(in: .global))
.frame(height: 36)
.border(Color.black, width: 1)
.padding(.horizontal)
...
}
Hope this can be of use, and thanks again for sharing your fix to our problem!
Best, Tarq