Search code examples
iosswiftswiftuinslayoutconstraint

Unable to simultaneously satisfy constraints?


I am developing a Log-In screen using XCode 15.2 and the latest version of SwiftUI that is shipped in Xcode.

The Simulator I use is the iPhone 15 Pro.

The Log-In Screen has two TextFields one contains the Users Email Address and the other is a SecureField which is used for the Password.

The code for the TextFields are:

TextField("Email Address", text: $userData.Email)
                        .frame(width: UIScreen.main.bounds.width - 100)
                        .textFieldStyle(.roundedBorder)
                        .textInputAutocapitalization(.never)
                        .opacity(0.8)

SecureField("Password", text: $userData.Password)
                        .frame(width: UIScreen.main.bounds.width - 100)
                        .textFieldStyle(.roundedBorder)
                        .opacity(0.8)

When I run this in the Simulator, all looks fine, but as soon as I click into either TextField, I see the following in the Debug area.

Error for queryMetaDataSync: 2
Error for queryMetaDataSync: 2
Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x6000021735c0 'accessoryView.bottom' _UIRemoteKeyboardPlaceholderView:0x106839040.bottom == _UIKBCompatInputView:0x1068374d0.top   (active)>",
    "<NSLayoutConstraint:0x6000021623f0 'assistantHeight' SystemInputAssistantView.height == 45   (active, names: SystemInputAssistantView:0x10681fc80 )>",
    "<NSLayoutConstraint:0x6000021731b0 'assistantView.bottom' SystemInputAssistantView.bottom == _UIKBCompatInputView:0x1068374d0.top   (active, names: SystemInputAssistantView:0x10681fc80 )>",
    "<NSLayoutConstraint:0x600002173110 'assistantView.top' V:[_UIRemoteKeyboardPlaceholderView:0x106839040]-(0)-[SystemInputAssistantView]   (active, names: SystemInputAssistantView:0x10681fc80 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600002173110 'assistantView.top' V:[_UIRemoteKeyboardPlaceholderView:0x106839040]-(0)-[SystemInputAssistantView]   (active, names: SystemInputAssistantView:0x10681fc80 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

For the life of me I don't understand what is driving this error. Reading the error it mentions 'remote keyboard'

What does get my attention is the following line:

"<NSLayoutConstraint:0x6000021623f0 'assistantHeight' SystemInputAssistantView.height == 45   (active, names: SystemInputAssistantView:0x10681fc80 )>"

I don't know if something needs to be a height of 45 or I have set a height of 45 somewhere, which I am unable to find anywhere.

Can someone please help me out with what I am seeing here.

Pete


Solution

  • Let's say you have three different views,

    1. UIRemoteKeyboardPlaceholderView
    2. UIKBCompatInputView
    3. SystemInputAssistantView

    And you added four constraints between these three views,

    1. UIRemoteKeyboardPlaceholderView's bottom edge should equal UIKBCompatInputView's top edge.
    2. SystemInputAssistantView's height should equal 45.
    3. SystemInputAssistantView's bottom edge should equal UIKBCompatInputView's top edge.
    4. SystemInputAssistantView's top edge should equal UIRemoteKeyboardPlaceholderView's bottom edge.

    Problem in your constraints

    1. As per your constraints, 3rd and 4th constraint signifies that your SystemInputAssistantView should be in between UIKBCompatInputView and UIRemoteKeyboardPlaceholderView
    2. 1st constraints signifies that UIKBCompatInputView should be at immediate bottom of UIRemoteKeyboardPlaceholderView

    And, that is why you are having constraint issues between three of your views. The layout engine is getting confused between these two constraints as it doesn't know which constraint should have a higher priority.

    Try and remove one of the constraints (either 1st one or 3rd and 4th one)and it should work fine.

    Here is a useful site that you can use to visualise your constraints, WTF Auto Layout