MacOS 应用程序上的 SwiftUI - 奇怪的焦点行为
SwiftUI on MacOS App - strange Focus behavior
我有这个小示例应用程序,它按预期工作。
Textfields 和 YellowTextfield 以预期的顺序获得焦点。
如果我在 YellowTextfield 的文本字段中添加 .cornerRadius(5.0),则焦点顺序会发生变化。
现在 YellowTextfield 获得了开始的焦点。
为什么会发生这种情况,我该如何预防?
struct TestFocusOrder: View {
@State var name = ""
@State var age = ""
@State var city = ""
@State var job = ""
var eyeColors = ["Red", "Green", "Blue", "Tartan"]
@State private var eyeColor = ""
var body: some View {
VStack{
TextField("Name", text: $name)
YellowTextField(title: "yellow field", text: $city)
TextField("age", text: $age)
Picker("eye color", selection: $eyeColor) {
ForEach(eyeColors, id: \.self) {
Text([=10=])
}
}
Text("\(name) age:\(age) eye color:\(eyeColor)").font(.footnote)
} .frame(width: 400, height: 400, alignment: .center)
}
}
struct YellowTextField: View {
var title: String
@Binding var text : String
var body: some View {
TextField(title, text: $text)
//.cornerRadius(5.0). // <== Uncomment this, then the focus order changes
}
}
不幸的是,.cornerRadius(5.0)
没有像 iOS
中那样在 MacOs
中圆化 TextField
的角
试试这个:
TextField(title, text: $text)
.textFieldStyle(RoundedBorderTextFieldStyle()) // <-- rounded
如需更好的定制:
TextField(title, text: $text)
.padding()
.overlay(
RoundedRectangle(cornerRadius: 5)
.stroke(Color.blue, lineWidth: 1)
)
我有这个小示例应用程序,它按预期工作。
Textfields 和 YellowTextfield 以预期的顺序获得焦点。
如果我在 YellowTextfield 的文本字段中添加 .cornerRadius(5.0),则焦点顺序会发生变化。
现在 YellowTextfield 获得了开始的焦点。
为什么会发生这种情况,我该如何预防?
struct TestFocusOrder: View {
@State var name = ""
@State var age = ""
@State var city = ""
@State var job = ""
var eyeColors = ["Red", "Green", "Blue", "Tartan"]
@State private var eyeColor = ""
var body: some View {
VStack{
TextField("Name", text: $name)
YellowTextField(title: "yellow field", text: $city)
TextField("age", text: $age)
Picker("eye color", selection: $eyeColor) {
ForEach(eyeColors, id: \.self) {
Text([=10=])
}
}
Text("\(name) age:\(age) eye color:\(eyeColor)").font(.footnote)
} .frame(width: 400, height: 400, alignment: .center)
}
}
struct YellowTextField: View {
var title: String
@Binding var text : String
var body: some View {
TextField(title, text: $text)
//.cornerRadius(5.0). // <== Uncomment this, then the focus order changes
}
}
不幸的是,.cornerRadius(5.0)
没有像 iOS
MacOs
中圆化 TextField
的角
试试这个:
TextField(title, text: $text)
.textFieldStyle(RoundedBorderTextFieldStyle()) // <-- rounded
如需更好的定制:
TextField(title, text: $text)
.padding()
.overlay(
RoundedRectangle(cornerRadius: 5)
.stroke(Color.blue, lineWidth: 1)
)