是否可以在 swiftUI for Mac 中禁用 TextField
周围的焦点环?
最佳答案
我也有这个问题,经过几个小时的摆弄,答案似乎是否定的。但是,可以包装 NSTextField 并去掉焦点环。
以下代码已在最新版本中经过测试。
struct CustomTextField: NSViewRepresentable {
@Binding var text: String
init(text: Binding<String>) {
_text = text
}
func makeNSView(context: Context) -> NSTextField {
let textField = NSTextField(string: text)
textField.delegate = context.coordinator
textField.isBordered = false
textField.backgroundColor = nil
textField.focusRingType = .none
return textField
}
func updateNSView(_ nsView: NSTextField, context: Context) {
nsView.stringValue = text
}
func makeCoordinator() -> Coordinator {
Coordinator { self.text = $0 }
}
final class Coordinator: NSObject, NSTextFieldDelegate {
var setter: (String) -> Void
init(_ setter: @escaping (String) -> Void) {
self.setter = setter
}
func controlTextDidChange(_ obj: Notification) {
if let textField = obj.object as? NSTextField {
setter(textField.stringValue)
}
}
}
}
关于macos - 在 SwiftUI 中禁用 macOS 对焦环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57448578/