简体   繁体   中英

SwiftUI Software keyboard + two TextFields = Constraint conflict

TL;DR

Tapping a second TextField while the keyboard is showing triggers a constraint conflict.

Details

The code below is runnable as-is. Tap the top TextField , then the one below. The Xcode console reports this (abridged for clarity):

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' ...>

Questions

AFAIK I have no control over any of the four constraints. Is there a way to avoid this? Should I even worry?

Code

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)
    }
}

have you resolve it yet? if not maybe this would help!
 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) }) } }

if you curious what happens to you constraints its because your keyboard had autocorrection in it.

using this line,
.disableAutocorrection(<#Bool?#>)
get rid of your autocorreaction tab in the top of your keyboard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM