In SwiftUI on iOS, I have the following .sheet
which displays a DisclosureGroup
which contains a DisclosureGroup
within it that has a TextField
. At the very bottom of the .sheet
are two buttons “Cancel” and “Enter” to allow the user to submit data / dismiss it:
import SwiftUI
struct TestView: View
{
@State private var sheetIsPresented: Bool = false
@State private var outerDisclosureIsExpanded: Bool = false
@State private var innerDisclosureIsExpanded: Bool = false
@State private var textFieldUserInput: String = ""
var body: some View
{
Button("OK")
{
self.sheetIsPresented = true
}
.sheet(isPresented: self.$sheetIsPresented)
{
VStack
{
Form
{
Section(header: Text("Section"))
{
DisclosureGroup("Outer Disclosure Group", isExpanded: self.$outerDisclosureIsExpanded)
{
ForEach(1...20, id: .self)
{
index in Text("Value (index)")
}
DisclosureGroup("Inner Disclosure Group", isExpanded: self.$innerDisclosureIsExpanded)
{
TextField("Enter name", text: self.$textFieldUserInput)
}
}
}
}
}
HStack
{
Button("Cancel")
{
}
.buttonStyle(.bordered).padding()
Spacer()
Button("Enter")
{
}
.buttonStyle(.borderedProminent)
.padding()
}
}
}
}
When I activate the TextField
contained in the inner DisclosureGroup
, the keyboard pushes up the “Cancel” and “Enter” buttons at the very bottom of the view, as well as adjusting the view so that the TextField
is visible as the user types:
I would like to make it so the “Cancel” and “Enter” buttons are not pushed up, that the keyboard goes over them so they are not visible, yet still pushes the View
up so that the TextField
is visible to the user as they type. .ignoresSafeArea(.keyboard, edges: .bottom)
does not work with this code for some reason, but even when I’ve tried it in other places of my code, it will push the keyboard on top of the TextField
as well as the “Cancel” and “Enter” buttons. Is there another way to accomplish what I’ve asked about?