iOS 14 SwiftUI Keyboard eleva a visualização automaticamente
Sep 19 2020
Estou usando TextField em minha visão e quando se torna o primeiro a responder, deixa a visão conforme mostrado no GIF abaixo.
Existe alguma maneira de me livrar desse comportamento?
Aqui está meu código
NavigationView(content: {
ZStack{
MyTabView(selectedIndex: self.$index)
.view(item: self.item1) {
NewView(title: "Hello1").navigationBarTitle("")
.navigationBarHidden(true)
}
.view(item: self.item2) {
NewView(title: "Hello2").navigationBarTitle("")
.navigationBarHidden(true)
}
.view(item: self.item3) {
NewView(title: "Hello3").navigationBarTitle("")
.navigationBarHidden(true)
}
}.navigationBarHidden(true)
.navigationBarTitle("")
}).ignoresSafeArea(.keyboard, edges: .bottom)
// New View
struct NewView:View {
@State var text:String = ""
var title:String
var body: some View {
VStack {
Spacer()
Text("Hello")
TextField(title, text: self.$text)
.textFieldStyle(RoundedBorderTextFieldStyle())
}.padding()
.onAppear {
debugPrint("OnApper \(self.title)")
}
}
}
Respostas
5 MojtabaHosseini Sep 18 2020 at 23:50
Você deve aplicar o modificador no ZStack, NÃO noNavigationView
NavigationView(content: {
ZStack{
,,,
}.navigationBarHidden(true)
.navigationBarTitle("")
.ignoresSafeArea(.keyboard, edges: .bottom) // <- This line moved up
})
Exemplo completo de trabalho:
struct ContentView: View {
@State var text = ""
var body: some View {
VStack{
Spacer()
Text("Hello, World")
TextField("Tap to test keyboard ignoring", text: $text)
.textFieldStyle(RoundedBorderTextFieldStyle())
}
.padding()
.ignoresSafeArea(.keyboard, edges: .bottom)
}
}
7 pawello2222 Sep 23 2020 at 02:13
Para .ignoresSafeAreafuncionar você precisa preencher toda a área disponível (por exemplo, usando a Spacer).
O seguinte não funcionará (sem espaçadores, apenas um TextField):
struct ContentView: View {
@State var text: String = ""
var body: some View {
VStack {
TextField("asd", text: self.$text)
.textFieldStyle(RoundedBorderTextFieldStyle())
}
.ignoresSafeArea(.keyboard, edges: .bottom)
}
}
No entanto, funcionará quando você adicionar Espaçadores (preencha todo o espaço disponível):
struct ContentView: View {
@State var text: String = ""
var body: some View {
VStack {
Spacer()
TextField("asd", text: self.$text)
.textFieldStyle(RoundedBorderTextFieldStyle())
Spacer()
}
.ignoresSafeArea(.keyboard, edges: .bottom)
}
}
Se não quiser usar espaçadores, você também pode usar GeometryReader:
struct ContentView: View {
@State var text: String = ""
var body: some View {
GeometryReader { _ in
...
}
.ignoresSafeArea(.keyboard, edges: .bottom)
}
}
O que significa um erro “Não é possível encontrar o símbolo” ou “Não é possível resolver o símbolo”?