SwiftUI - Why does the keyboard pushes my view?

天大地大妈咪最大 提交于 2021-02-11 06:35:29

问题


Why do I have the keyboard that pushes up my View? Here's the code:

import SwiftUI

struct ContentView : View {
    @State var searchText = ""
    @State var pressedFirstButton = false
    @State var pressedSecondButton = true
    @State var pressedThirdButton = false
    
    
    var body : some View {
        NavigationView {
            VStack {
                if pressedSecondButton == true {
                Form {
                    SearchBar(testo: $searchText)
                        .padding(.horizontal, -10)
                    
                    Text("ForEach")
                }
                }
                HStack {
                    Spacer()
                    buttonFirst
                    Spacer()
                    buttonSecond
                    Spacer()
                    buttonThird
                    Spacer()
                }
                
            }.navigationBarTitle("Search")
        }
    }
    
    var buttonFirst : some View {
        Button(action: {
            self.pressedFirstButton = true
            self.pressedSecondButton = false
            self.pressedThirdButton = false
        }) { if pressedFirstButton == true {
            ZStack {
                Rectangle()
                    .frame(width: 37, height: 37)
                    .foregroundColor(.clear)
                Image(systemName: "pencil.circle.fill")
                    .foregroundColor(.green)
                    .font(.system(size: 35))
            }.animation(.easeInOut)
            .padding(10)
        } else {
            ZStack {
                Rectangle()
                    .frame(width: 37, height: 37)
                    .foregroundColor(.clear)
                Image(systemName: "pencil.circle")
                    .foregroundColor(.black)
                    .font(.system(size: 35))
            }.padding(10)
        }
        }.animation(.easeInOut)
    }
    var buttonSecond : some View {
        Button(action: {
            self.pressedFirstButton = false
            self.pressedSecondButton = true
            self.pressedThirdButton = false
        }) { if pressedSecondButton == true {
            ZStack {
                Rectangle()
                    .frame(width: 37, height: 37)
                    .foregroundColor(.clear)
                Image(systemName: "magnifyingglass")
                    .foregroundColor(.green)
                    .font(.system(size: 35))
            }.animation(.easeInOut)
            .padding(10)
        } else {
            ZStack {
                Rectangle()
                    .frame(width: 37, height: 37)
                    .foregroundColor(.clear)
                Image(systemName: "magnifyingglass")
                    .foregroundColor(.black)
                    .font(.system(size: 35))
            }.padding(10)
        }
        }.animation(.easeInOut)
    }
    
    var buttonThird : some View {
        Button(action: {
            self.pressedFirstButton = false
            self.pressedSecondButton = false
            self.pressedThirdButton = true
        }) { if pressedThirdButton == true {
            ZStack {
                Rectangle()
                    .frame(width: 37, height: 37)
                    .foregroundColor(.clear)
                Image(systemName: "person.fill")
                    .foregroundColor(.green)
                    .font(.system(size: 35))
            }.animation(.easeInOut)
            .padding(10)
        } else {
            ZStack {
                Rectangle()
                    .frame(width: 37, height: 37)
                    .foregroundColor(.clear)
                Image(systemName: "person")
                    .foregroundColor(.black)
                    .font(.system(size: 35))
            }.padding(10)
        }
        }.animation(.easeInOut)
    }
    
}

And the SearchBar is on another Swift file and it's like this:

import SwiftUI
 
struct SearchBar: View {
    
    @Binding var testo : String
    @State private var isEditing = false
    @Environment(\.colorScheme) var colorScheme

    
    var body: some View {
        HStack {
            CustomTextField(placeholder: Text("Cerca...")
                .foregroundColor(colorScheme == .dark ? Color.white : Color.gray), text: $testo)
                .font(.custom("Ubuntu-Regular", size:17))
                .padding(.horizontal, 35)
                .background(Color(.clear))
                .cornerRadius(8)
                .overlay(
                    HStack {
                        Image(systemName: "magnifyingglass")
                            .foregroundColor(colorScheme == .dark ? Color.white : Color.gray)
                            .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
                            .padding(.leading, 8)
                    }
                )
                .onTapGesture {
                    self.isEditing = true
                }
 
            if isEditing {
                Button(action: {
                    UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
                    self.isEditing = false
                    self.testo = ""

                }) {
                    HStack {
                        Image(systemName: "multiply.circle.fill")
                            .foregroundColor(colorScheme == .dark ? Color.white : Color.gray)
                    Text("Annulla")
                        .font(.custom("Ubuntu-Regular", size:17))
                        .foregroundColor(.green)
                    }
                }
                .padding(.trailing, 10)
                .transition(.move(edge: .trailing))
                .animation(.easeInOut)
            }
        }
    }
}

struct CustomTextField: View {
    var placeholder: Text
    @Binding var text: String
    var editingChanged: (Bool)->() = { _ in }
    var commit: ()->() = { }

    var body: some View {
        ZStack(alignment: .leading) {
            if text.isEmpty { placeholder }
            TextField("", text: $text, onEditingChanged: editingChanged, onCommit: commit)
        }
    }
}

And this is what happens when the keyboard comes up:

How can I avoid having the bar with those buttons being pushed up by the keyboard?

Thanks to everyone who will help me!!


回答1:


  • By default SwiftUI view doesn't ignore safe-area .

when keyboard come up the area belongs to safe area. so that you can ignore it using .ignoresSafeArea(.keyboard, edges: .bottom)

VStack {
         ...  
          }.navigationBarTitle("Search")
           .ignoresSafeArea(.keyboard, edges: .bottom) //<- here


来源:https://stackoverflow.com/questions/65456168/swiftui-why-does-the-keyboard-pushes-my-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!