2

Environment: Xcode Version 12.4 (12D4e)

Scenario: I found one area that apparently has constraint violations.
I narrowed it down to the .navigationTitle.

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack {
                Text("Hello World")
            }.navigationTitle("Turkey")
        }
    }
}

That's it.
If I remove the .navigationTitle(), it clears up.

Here's are the constraint violation(s) listed in the console:


2021-02-09 19:14:38.578190-0800 NavBarCheck[6300:437683] [LayoutConstraints] 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:0x600003149ef0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x12c01a770]-(6)-[_UIModernBarButton:0x12c0191e0'Turkey']   (active)>",
    "<NSLayoutConstraint:0x600003149f40 'CB_Trailing_Trailing' _UIModernBarButton:0x12c0191e0'Turkey'.trailing <= _UIButtonBarButton:0x12c018560.trailing   (active)>",
    "<NSLayoutConstraint:0x60000314acb0 'UINav_static_button_horiz_position' _UIModernBarButton:0x12c01a770.leading == UILayoutGuide:0x600002b65260'UIViewLayoutMarginsGuide'.leading   (active)>",
    "<NSLayoutConstraint:0x60000314ad00 'UINavItemContentGuide-leading' H:[_UIButtonBarButton:0x12c018560]-(0)-[UILayoutGuide:0x600002b65180'UINavigationBarItemContentLayoutGuide']   (active)>",
    "<NSLayoutConstraint:0x60000314c780 'UINavItemContentGuide-trailing' UILayoutGuide:0x600002b65180'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x12ae0f100.trailing   (active)>",
    "<NSLayoutConstraint:0x60000314b3e0 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x12ae0f100.width == 0   (active)>",
    "<NSLayoutConstraint:0x60000314cb40 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x600002b65260'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UINavigationBarContentView:0x12ae0f100 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600003149ef0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x12c01a770]-(6)-[_UIModernBarButton:0x12c0191e0'Turkey']   (active)>

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.

Question: Is this a bug? or if not, what's happening and remedy?

jnpdx
  • 45,847
  • 6
  • 64
  • 94
Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
  • 1
    There's a reasonable number of questions about this here on SO if you search for "SwiftUI constraints" The general consensus seems to be to not be concerned with it – jnpdx Feb 10 '21 at 03:37

2 Answers2

3

NavigationBarTitle and NavigationTitle are deprecated from iOS 14.3. Try adding .navigationViewStyle(StackNavigationViewStyle()) on the navigationView.

All credits to this solution to https://stackoverflow.com/a/66024249/15184473

Jacob Kim
  • 51
  • 1
3

I have studied this question for a long time and this is what I came to. I think it will be useful:

One of the interesting things about Navigation View is how it also performs the split-screen function on large devices-usually iPhones and large iPads.

var body: some View {
        NavigationView {
            Text("Primary")
        }
    }

If you rotate to landscape orientation (Cmd + ->) of the iPhone 11 ProMax, you will see that the text view disappears.

SwiftUI automatically considers landscape navigation views and shows DetailView instead of main ("Primary").

You can solve the problem the way SwiftUI expects by providing two views inside your NavigationView, for instance:

var body: some View {
        NavigationView {
            Text("Primary")
            Text("Secondary")
        }
    }

Since I have little reputation, I can only post a photo with a link (If my post was useful, please rate it): enter image description here

This is why the error "Unable to simultaneously satisfy constraints" is thrown if we add. navigation Bar Title(Text ("Today's Flavors"), displayMode:. inline)

Therefore, the solution is as follows:

  1. Add a second view to the Navigation Vi

Here, when you rotate the screen, screen # 2 - Text("Secondary") will be displayed.

    var body: some View {
            NavigationView {
                Text("Primary")
                Text("Secondary")
            .navigationBarTitle(Text("Today's Flavors"), displayMode: .inline)
            }
    }
  1. Modifier .navigationViewStyle(StackNavigationViewStyle())

However, if you want to explicitly specify that the first screen (Text("Primary")) is always displayed when you rotate, then you need to add the .navigationViewStyle(StackNavigationViewStyle) modifier()) , which allows you to always switch to Text ("Primary"), regardless of the screen.

var body: some View {
        NavigationView {
            Text("Primary")
            Text("Secondary")
        .navigationBarTitle(Text("Today's Flavors"), displayMode: .inline)
        }
        .navigationViewStyle(StackNavigationViewStyle())

Also, you can read, more about NavigationView here

Eldar
  • 458
  • 3
  • 13