SwiftUI 软件键盘 + 两个 TextFields = 约束冲突
SwiftUI Software keyboard + two TextFields = Constraint conflict
TL;DR
在显示键盘时再次点击 TextField
会触发约束冲突。
详情
下面的代码可以按原样运行。点击顶部的 TextField
,然后点击下方的。 Xcode 控制台报告了这一点(为清楚起见进行了删减):
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
<'assistantHeight' TUISystemInputAssistantView.height == 44 (active)>
<'assistantView.bottom' TUISystemInputAssistantView.bottom == _UIKBCompatInputView.top (active)>
<'assistantView.top' V:|-(0)-[TUISystemInputAssistantView] (active, names: '|':UIInputSetHostView )>
<'inputView.top' V:|-(0)-[_UIKBCompatInputView] (active, names: '|':UIInputSetHostView )>
Will attempt to recover by breaking constraint
<'assistantView.bottom' ...>
问题
AFAIK 我无法控制四个约束中的任何一个。有没有办法避免这种情况?我应该担心吗?
代码
struct ContentView: View {
@State private var text1 = ""
@State private var text2 = ""
var body: some View {
VStack {
TextField("Tap here first...", text: $text1)
.background(Color.white).padding()
TextField("...then tap here", text: $text2)
.background(Color.white).padding()
Spacer()
}.background(Color.gray)
}
}
你解决了吗?
如果没有,也许这会有所帮助!
GeometryReader.init { geometry in
ScrollView.init {
VStack.init(alignment: .leading, spacing: nil, content: {
Text("Hello.")
.font(.custom("Helvetica-Bold", size: 32))
Text("This just my sample application using swift UI")
.padding(.init(top: 0, leading: 0, bottom: 0, trailing: (geometry.size.width * 0.35)))
.foregroundColor(.gray)
TextField.init("Email Address", text: self.$textFieldName)
.frame(width: geometry.size.width, height: 50)
.disableAutocorrection(true)
.layoutPriority(1000)
TextField.init("Email Address", text: self.$textFieldName)
.frame(width: geometry.size.width, height: 50)
.disableAutocorrection(true)
.layoutPriority(1000)
})
}
}
如果你好奇发生了什么,你会限制它,因为你的键盘有自动更正功能。
使用这条线,
.disableAutocorrection(<#Bool?#>)
摆脱键盘顶部的自动更正选项卡。
TL;DR
在显示键盘时再次点击 TextField
会触发约束冲突。
详情
下面的代码可以按原样运行。点击顶部的 TextField
,然后点击下方的。 Xcode 控制台报告了这一点(为清楚起见进行了删减):
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
<'assistantHeight' TUISystemInputAssistantView.height == 44 (active)>
<'assistantView.bottom' TUISystemInputAssistantView.bottom == _UIKBCompatInputView.top (active)>
<'assistantView.top' V:|-(0)-[TUISystemInputAssistantView] (active, names: '|':UIInputSetHostView )>
<'inputView.top' V:|-(0)-[_UIKBCompatInputView] (active, names: '|':UIInputSetHostView )>
Will attempt to recover by breaking constraint
<'assistantView.bottom' ...>
问题
AFAIK 我无法控制四个约束中的任何一个。有没有办法避免这种情况?我应该担心吗?
代码
struct ContentView: View {
@State private var text1 = ""
@State private var text2 = ""
var body: some View {
VStack {
TextField("Tap here first...", text: $text1)
.background(Color.white).padding()
TextField("...then tap here", text: $text2)
.background(Color.white).padding()
Spacer()
}.background(Color.gray)
}
}
你解决了吗? 如果没有,也许这会有所帮助!
GeometryReader.init { geometry in
ScrollView.init {
VStack.init(alignment: .leading, spacing: nil, content: {
Text("Hello.")
.font(.custom("Helvetica-Bold", size: 32))
Text("This just my sample application using swift UI")
.padding(.init(top: 0, leading: 0, bottom: 0, trailing: (geometry.size.width * 0.35)))
.foregroundColor(.gray)
TextField.init("Email Address", text: self.$textFieldName)
.frame(width: geometry.size.width, height: 50)
.disableAutocorrection(true)
.layoutPriority(1000)
TextField.init("Email Address", text: self.$textFieldName)
.frame(width: geometry.size.width, height: 50)
.disableAutocorrection(true)
.layoutPriority(1000)
})
}
}
如果你好奇发生了什么,你会限制它,因为你的键盘有自动更正功能。
使用这条线,
.disableAutocorrection(<#Bool?#>)
摆脱键盘顶部的自动更正选项卡。