英文:
How to show view conditionally when the keyboard appears swiftUI
问题
我想在点击textField并出现键盘时展示不同的视图。我尝试使用.onReceive修饰符以及keyboardWillHide/ShowNotification属性,但这些方法都没有按预期工作。以下是代码:
struct ContentView: View {
@State private var text = ""
var body: some View {
VStack {
Spacer()
if //isKeyboardVisible - 需要实现 {
Text("键盘已显示")
} else {
VStack(alignment: .center, spacing: 16) {
Text("输入提示")
.font(.custom("Avenir Next Medium", size: 18))
.foregroundColor(.white)
.padding(.trailing, 230)
TextField(text: $text)
.frame(height: 120)
.cornerRadius(10)
Text("创建")
.opacity(text.isEmpty ? 0.4 : 1)
}
.padding(.horizontal)
}
}
}
}
英文:
I would like to show a different view when I tap on the textField and the keyboard appears. I tried using .onReceive modifiers along with keyboardWillHide/ShowNotification properties, but none of those approaches work as expected. Here is the code:
struct ContentView: View {
@State private var text = ""
var body: some View {
VStack {
Spacer()
if //isKeyboardVisible - need to implement {
{
Text("Keyboard is shown")
} else {
VStack(alignment: .center, spacing: 16) {
Text("Enter prompts")
.font(.custom("Avenir Next Medium", size: 18))
.foregroundColor(.white)
.padding(.trailing, 230)
TextField(text: $text)
.frame(height: 120)
.cornerRadius(10)
Text("create")
.opacity(text.isEmpty ? 0.4 : 1)
}
.padding(.horizontal)
}
}
}
}
答案1
得分: 1
The @FocusState
属性将允许您跟踪键盘何时显示,另一方面,如果尝试在 SwiftUI 中使用 IF ELSE
,应用程序将崩溃,因为动画重叠以及 if else 在 SwiftUI 中的工作方式。以下是一个示例,可以帮助您解决问题。
import SwiftUI
struct KeyboardView: View {
@State private var text = ""
@FocusState private var keyboardShown: Bool
var body: some View {
ZStack {
Spacer()
VStack(alignment: .center, spacing: 16) {
Text("输入提示")
.font(.custom("Avenir Next Medium", size: 18))
.foregroundColor(.white)
.padding(.trailing, 230)
TextField("输入文本", text: $text)
.frame(height: 120)
.cornerRadius(10)
.focused($keyboardShown)
Text("创建")
.opacity(text.isEmpty ? 0.4 : 1)
}
.padding(.horizontal)
if keyboardShown
{
ZStack {
Color.blue.edgesIgnoringSafeArea(.all)
Text("键盘已显示")
.onTapGesture {
keyboardShown = false
}
}
}
}
}
}
请注意,我已将代码中的 "
替换为正常的双引号 "
.
英文:
The @FocusState
property will allow you to track when the keyboard is shown, on the other hand if try this with IF ELSE
the app will crash due an overlap of animations and the way the if else works in swiftUI. Here is an example that can help you out.
import SwiftUI
struct KeyboardView: View {
@State private var text = ""
@FocusState private var keyboardShown: Bool
var body: some View {
ZStack {
Spacer()
VStack(alignment: .center, spacing: 16) {
Text("Enter prompts")
.font(.custom("Avenir Next Medium", size: 18))
.foregroundColor(.white)
.padding(.trailing, 230)
TextField("Enter text", text: $text)
.frame(height: 120)
.cornerRadius(10)
.focused($keyboardShown)
Text("create")
.opacity(text.isEmpty ? 0.4 : 1)
}
.padding(.horizontal)
if keyboardShown
{
ZStack {
Color.blue.edgesIgnoringSafeArea(.all)
Text("Keyboard is shown")
.onTapGesture {
keyboardShown = false
}
}
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论