I am trying to build a sheet that has a textfield which (when focused) opens the keyboard and also has some further content as well. I want the presentationDetents from the sheet to be the height of the content inside the sheet and when the keyboard appears the sheet should expand its height to fit the keyboard at the bottom plus the actual content on top of it. So in short: The sheet should always be the size of its content and when the keyboard appears it should be the size of the content plus the size of the keyboard.
I have already tried using a GeometryReader but when I use .presentationDetents([.height(geometry.size.height)])
the bottom sheet is too small to show the content (see attached photo). I also tried using preference keys, but couldn't make that work either...
This is my (basic) code:
struct TestView: View {
@State private var showSheet: Bool = false
@State private var textfieldInput: String = ""
var body: some View {
VStack {
Button("Show Sheet") {
showSheet.toggle()
}
.sheet(isPresented: $showSheet) {
GeometryReader { geometry in
VStack (alignment: .leading) {
TextField("This is a textfield", text: $textfieldInput)
.bold()
.font(.title3)
Text("More (variable) content underneath the textfield")
}
.padding(20)
.border(Color.red)
.presentationDetents([.height(geometry.size.height)])
}
}
}
}
}
Result with GeometryReader Result without GeometryReader and presentationDetents
I would be very happy for any ideas / suggestions!