如何解决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)
}
}
而 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。