1

SwiftUi am learning new. I created a tab bar, but when the keyboard is turned on, the tabbar goes up with the keyboard. How can I fix it. Can you help me? I want to fix the tab bar to the bottom. Or is there something that can get the keyboard to the top of the pages? Since I did not know where to fix it, I had to throw out all the codes.

struct ContentView: View {
  
  @State var selected = 0
  
  var body: some View {
      
      VStack{
        
        if self.selected == 0{
            Color.blue
        }
        else if self.selected == 1{
            Color.red
        }
        else if self.selected == 2{
            Color.pink
        }
        else if self.selected == 3{
            Color.green
        }
        else if self.selected == 4{
            Color.yellow
        }
        
        Spacer()
          
          ZStack(alignment: .top){
              
              BottomBar(selected: self.$selected)
                  .padding()
                  .padding(.horizontal, 22)
                  .background(CurvedShape())
              
              Button(action: {
                self.selected = 4
                  
              }) {
                  
                  Image(systemName:"heart.fill").renderingMode(.original).padding(18)
                  
              }.background(Color.yellow)
              .clipShape(Circle())
              .offset(y: -32)
              .shadow(radius: 5)
              
          }
          
          
      }.background(Color("Color").edgesIgnoringSafeArea(.top))
  }
 }

 struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
      ContentView()
  }
 }

 struct CurvedShape : View {
  
  var body : some View{
      
      Path{path in
          
          path.move(to: CGPoint(x: 0, y: 0))
          path.addLine(to: CGPoint(x: UIScreen.main.bounds.width, y: 0))
          path.addLine(to: CGPoint(x: UIScreen.main.bounds.width, y: 55))
          
          path.addArc(center: CGPoint(x: UIScreen.main.bounds.width / 2, y: 55), 
radius: 30, startAngle: .zero, endAngle: .init(degrees: 180), clockwise: true)
          
          path.addLine(to: CGPoint(x: 0, y: 55))
          
      }.fill(Color.white)
      .rotationEffect(.init(degrees: 180))
  }
 }

 struct BottomBar : View {
  
  @Binding var selected : Int
  
  var body : some View{
      
      HStack{
          
          Button(action: {
              
              self.selected = 0
              
          }) {
              
              Image(systemName:"lasso")
              
          }.foregroundColor(self.selected == 0 ? .black : .gray)
          
          Spacer(minLength: 12)
          
          
          Button(action: {
              
              self.selected = 1
              
          }) {
              
              Image(systemName: "trash")
              
          }.foregroundColor(self.selected == 1 ? .black : .gray)
          
          
          Spacer().frame(width: 120)
          
          Button(action: {
              
              self.selected = 2
              
          }) {
              
              Image(systemName:"person")
              
          }.foregroundColor(self.selected == 2 ? .black : .gray)
          .offset(x: -10)

          
          Spacer(minLength: 12)
          
          Button(action: {
              
              self.selected = 3
              
          }) {
              
              Image(systemName: "pencil")
              
          }.foregroundColor(self.selected == 3 ? .black : .gray)
      }
   }
  }
metaflor22
  • 165
  • 1
  • 9

1 Answers1

3

Set

 .ignoresSafeArea(.keyboard)

To content view.

Raja Kishan
  • 16,767
  • 2
  • 26
  • 52