macos - SwiftUI - MacOS - 警报中的文本字段未获得焦点

标签 macos swiftui alert

我在 MacOS 的警报中添加了一个文本字段。但是,当警报显示时,TextField 不会获得焦点。这是我测试过的代码。这可能吗?如果这是一个错误,请建议其他解决方法。

import SwiftUI

struct ContentView: View {
    @State private var presented = false
    @State private var username = ""
    @FocusState private var focused: Bool
    
    var body: some View {
        VStack {
            Button(action: {
                focused = true
                presented = true
            }, label: {
                Text("Click to show Alert")
            })
        }
        .alert("", isPresented: $presented, actions: {
            VStack {
                TextField("User name (email address)", text: $username)
                    .focusable()
                    .focused($focused)
                Button(action: {}, label: { Text("OK") })
            }
            .onAppear() {
                // Try setting focus with a delay.
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
                    focused = true
                })
            }
        }, message: {
            Text("The textField in this alert doesn't get the focus. Therefore, a manual click is needed to set focus and start typing.")
        })
    }
}

最佳答案

我可以确认焦点在 Alert 中不起作用。可能的解决方法是使用自定义 Sheet:

struct ContentView: View {
    @State private var presented = false
    @State private var username = ""
    @FocusState private var focused: Bool

    var body: some View {
        VStack {
            Button(action: {
                presented = true
                focused = true
            }, label: {
                Text("Click to show Sheet")
            })
        }
        
        .sheet(isPresented: $presented, content: {
            
            VStack {
                // App Icon placeholder
                RoundedRectangle(cornerRadius: 8)
                    .fill(.secondary)
                    .frame(width: 48, height: 48)
                    .padding(12)
                
                Text("The textField in this alert doesn't get the focus. Therefore, a manual click is needed to set focus and start typing.")
                    .font(.caption)
                    .multilineTextAlignment(.center)

                TextField("User name (email address)", text: $username)
                    .focused($focused)
                    .padding(.vertical)
                
                Button(action: {
                    presented = false
                }, label: {
                    Text("OK")
                        .frame(maxWidth: .infinity)
                })
                .buttonStyle(.borderedProminent)
            }
            .padding()
            .frame(width: 260)
        })
    }
}

关于macos - SwiftUI - MacOS - 警报中的文本字段未获得焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74831978/

相关文章:

macos - 从 Gitlab 实例克隆时,Jenkins 在 Mac slave 上挂起

python - 在 Snow Leopard 上为 Python 2.7 安装 MYSQL-python-1.2.3 时出现问题

ios - 快速组合 : Unable to infer complex closure return type error

javascript - 如果在 PHP、JS 中登录失败但发现问题,我想生成一个警报对话框

javascript - Jquery if 条件,如果字符串格式不正确,将调用警报

macos - 如何在 Mac 上的 IOBluetooth 框架中获取更具描述性的错误

java - 无法在 netbeans 中添加 apache tomcat 服务器

ios - SwiftUI 以编程方式选择列表项

ios - 如何让 SwiftUI 全屏显示?

javascript 通过返回 true/false 确认替换