How to avoid View moving off the screen when keyboard appears in SwiftUI?

Viewed 582

absolute greenhorn writing here: i have a very basic app that does some calculations. It has only one view that has some Stacks in it that then contain text fields. underneath are two buttons and an image. when I now click on a text field, the view moves up and off the screen even though I want to make the keyboard over the buttons and image as I do not need them visible when the keyboard is active. I have tried to apply the .ignoresSafeArea(.keyboard)addition to basically everything in the view but it still does it.

Screenshot with Keyboard up
Screenshot with Keyboard up

    VStack{
        Spacer()
        HStack (alignment: .top){
            
            VStack (alignment: .trailing, spacing:8){
                
                Text("Volumenstrom")
                    .font(.body)
                    .fontWeight(.bold)
                    .multilineTextAlignment(.center)
                    .frame(height: 50)
                    .ignoresSafeArea(.keyboard)
                Text("Geschwindigkeit")
                    .font(.body)
                    .fontWeight(.bold)
                    .multilineTextAlignment(.center)
                    .frame(height: 50)
                    .ignoresSafeArea(.keyboard)
                Text("Kanalmaß A")
                    .font(.body)
                    .fontWeight(.bold)
                    .multilineTextAlignment(.center)
                    .frame(height: 50)
                    .ignoresSafeArea(.keyboard)
                Text("Kanalmaß B")
                    .font(.body)
                    .fontWeight(.bold)
                    .multilineTextAlignment(.center)
                    .frame(height: 50)
                    .ignoresSafeArea(.keyboard)
                Text("Durchmesser D")
                    .font(.body)
                    .fontWeight(.bold)
                    .multilineTextAlignment(.center)
                    .frame(height: 50)
                    .ignoresSafeArea(.keyboard)
                
            }
            
            VStack (alignment: .center){
                
                TextField("0", text: $VolumenstromEingabe,
                          onCommit : { self.BerechungVG1(); self.BerechungVG2()} )
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .modifier(ClearButton(text: $VolumenstromEingabe))
                    .frame(width: 100, height: 50)
                    .keyboardType(.numbersAndPunctuation)
                    .ignoresSafeArea(.keyboard)
                
                TextField("0", text: $GeschwindigkeitEingabe,
                          onCommit: {self.BerechungVG1(); self.BerechungVG2()})
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .modifier(ClearButton(text: $GeschwindigkeitEingabe))
                    .frame(width: 100, height: 50)
                    .keyboardType(.numbersAndPunctuation)
                    .ignoresSafeArea(.keyboard)
                
                
                TextField("0", text: $KanalAEingabe,
                          onCommit: { self.BerechungVGA()})
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .modifier(ClearButton(text: $KanalAEingabe))
                    .frame(width: 100, height: 50)
                    .keyboardType(.numbersAndPunctuation)
                    .ignoresSafeArea(.keyboard)
                
                
                TextField("0", text: $KanalBEingabe,
                          onCommit: { self.BerechungVAB(); self.BerechungVGD()
                })
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .modifier(ClearButton(text: $KanalBEingabe))
                    .frame(width: 100, height: 50)
                    .keyboardType(.numbersAndPunctuation)
                    .ignoresSafeArea(.keyboard)
                
                
                TextField("0", text: $KanalDErgebnis,
                          onCommit: { self.BerechungDg(); self.BerechungVG1()})
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .modifier(ClearButton(text: $KanalDErgebnis))
                    .frame(width: 100, height: 50)
                    .keyboardType(.numbersAndPunctuation)
                    .ignoresSafeArea(.keyboard)
                
                
            }
            
            VStack (alignment: .leading, spacing: 8){
                
                Text("in m³/h")
                    .font(.body)
                    .fontWeight(.bold)
                    .frame(height: 50)
                    .ignoresSafeArea(.keyboard)
                Text("in m/s")
                    .font(.body)
                    .fontWeight(.bold)
                    .frame(height: 50)
                    .ignoresSafeArea(.keyboard)
                Text("in mm")
                    .font(.body)
                    .fontWeight(.bold)
                    .frame(height: 50)
                    .ignoresSafeArea(.keyboard)
                Text("in mm")
                    .font(.body)
                    .fontWeight(.bold)
                    .frame(height: 50)
                    .ignoresSafeArea(.keyboard)
                Text("in mm")
                    .font(.body)
                    .fontWeight(.bold)
                    .frame(height: 50)
                    .ignoresSafeArea(.keyboard)
            }
            
            
            
        }
        Button(action:  {
            self.ClearAll()
        
        
        })   {
            Text("Reset")}
        
        
        
        Spacer()
        
        
        HStack{
            
            VStack{
                Spacer()
                Image("Logo Martin neu-60")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(height: 80)
                    .padding()
                    .ignoresSafeArea(.keyboard)
                
                Button(action: {
                    self.showingInfoSheet.toggle()
                }) {
                    Text("Informationen")
                        .ignoresSafeArea(.keyboard)
                    
                }
                .sheet(isPresented: $showingInfoSheet, content: {
                    ScrollView1()
                })
                
                Text("Luftkanalrechner v1.0, Build 6")
                    .padding()
                    .ignoresSafeArea(.keyboard)
            }
        }
    }
0 Answers
Related