Pan*_*ruz 7 keyboard searchbar swift swiftui
为什么我的键盘会推高我的视图?这是代码:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
SearchBar 位于另一个 Swift 文件中,如下所示:
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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
这就是键盘出现时发生的情况:
如何避免带有这些按钮的栏被键盘推起?
感谢所有帮助我的人!!
Yod*_*ama 14
当键盘出现时该区域属于safe area。这样你就可以使用忽略它.ignoresSafeArea(.keyboard, edges: .bottom)
VStack {
...
}.navigationBarTitle("Search")
.ignoresSafeArea(.keyboard, edges: .bottom) //<- here
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5067 次 |
| 最近记录: |