SwiftUI-为什么键盘会推动我的视野? [英] SwiftUI - Why does the keyboard pushes my view?

查看:63
本文介绍了SwiftUI-为什么键盘会推动我的视野?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的键盘可以推动我的View?这是代码:

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)
    }
    
}

SearchBar在另一个Swift文件上,就像这样:

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!!

推荐答案

  • 默认情况下,SwiftUI视图不会忽略安全区域.
  • 键盘出现时,该区域属于安全区域.这样您就可以使用 .ignoresSafeArea(.keyboard,edge:.bottom)

    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
    

    这篇关于SwiftUI-为什么键盘会推动我的视野?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆