SwiftUI - 为什么键盘会推动我的视图?
SwiftUI - Why does the keyboard pushes my view?
为什么我的键盘会上推我的视图?这是代码:
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 文件上,它是这样的:
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)
}
}
}
这就是键盘出现时发生的情况:
如何避免带有这些按钮的栏被键盘向上推?
感谢所有帮助我的人!!
- 默认情况下,SwiftUI 视图不会忽略安全区域。
当键盘出现时,该区域属于 safe area
。这样您就可以使用 .ignoresSafeArea(.keyboard, edges: .bottom)
忽略它
VStack {
...
}.navigationBarTitle("Search")
.ignoresSafeArea(.keyboard, edges: .bottom) //<- here
为什么我的键盘会上推我的视图?这是代码:
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 文件上,它是这样的:
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)
}
}
}
这就是键盘出现时发生的情况:
如何避免带有这些按钮的栏被键盘向上推?
感谢所有帮助我的人!!
- 默认情况下,SwiftUI 视图不会忽略安全区域。
当键盘出现时,该区域属于 safe area
。这样您就可以使用 .ignoresSafeArea(.keyboard, edges: .bottom)
VStack {
...
}.navigationBarTitle("Search")
.ignoresSafeArea(.keyboard, edges: .bottom) //<- here