'SwiftUI LayoutConstraints NavigationView navigationViewStyle

I've got a SwiftUI View containing a NavigationView and some NavigationLinks. For some reason I got a error message regarding LayoutConstraints. This error is also discussed in this SO Question, with recommended solution setting the navigationViewStyle by .navigationViewStyle(StackNavigationViewStyle()) or .navigationViewStyle(.stack).

However by setting the navigationViewStyle to StackNavigation, the Layout is modified in a non intended way. Using StackNavigation will change the Layout on both IOS and IPadOS to a simple List. Not configuring navigationViewStyle or setting it to .automatic will lead to having a List on IOS and a multiple columns on IPadOS.

Additionally the LayoutConstraints error seems to be a error only occur on IOS.

How can I get rid of the LayoutConstraints error (on IOS) without using StackNavigation? Is it possible to distinguish between IOS/IPadOS?

My View:

struct ProgramView: View {
    @ObservedObject var itemViewModel: ItemViewModel
 
    var body: some View {
        NavigationView {
            VStack {
                ForEach(itemViewModel.filteredSessions, id: \.id) { session in
                    NavigationLink(
                        destination: SessionDetail(session: session)
                    ) {
                        SessionRow(session: session)
                    }
                }
                // shortened for brevity
            }
            .navigationTitle("Program")
            .navigationBarItems(trailing: UpdateApiButton(itemViewModel: itemViewModel))
        }
        .navigationViewStyle(.automatic) // default behaviour
        .navigationViewStyle(.stack) // Stack; shows a List for IOS+IPadOS; IPadOS should remain multi-column
        .navigationViewStyle(.columns) // Uses multi-column also in IOS
    }
}

The LayoutConstraints error:

2022-04-06 20:43:49.474918+0200 XXX[1505:41050] [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:0x282b8b2f0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x104c1f610]-(6)-[_UIModernBarButton:0x104c1d7e0'Program']   (active)>",
    "<NSLayoutConstraint:0x282b8b340 'CB_Trailing_Trailing' _UIModernBarButton:0x104c1d7e0'Program'.trailing <= _UIButtonBarButton:0x104c1d400.trailing   (active)>",
    "<NSLayoutConstraint:0x282b813b0 'UINav_static_button_horiz_position' _UIModernBarButton:0x104c1f610.leading == UILayoutGuide:0x2831b9a40'UIViewLayoutMarginsGuide'.leading   (active)>",
    "<NSLayoutConstraint:0x282b8b7a0 'UINavItemContentGuide-leading' H:[_UIButtonBarButton:0x104c1d400]-(6)-[UILayoutGuide:0x2831b9960'UINavigationBarItemContentLayoutGuide']   (active)>",
    "<NSLayoutConstraint:0x282b89680 'UINavItemContentGuide-trailing' UILayoutGuide:0x2831b9960'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x104c1c430.trailing   (active)>",
    "<NSLayoutConstraint:0x282bfce60 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x104c1c430.width == 0   (active)>",
    "<NSLayoutConstraint:0x282b89a40 'UIView-leftMargin-guide-constraint' H:|-(8)-[UILayoutGuide:0x2831b9a40'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UINavigationBarContentView:0x104c1c430 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x282b8b2f0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x104c1f610]-(6)-[_UIModernBarButton:0x104c1d7e0'Program']   (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.
2022-04-06 20:43:49.476126+0200 XXX[1505:41050] [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:0x282b8a760 UIView:0x104c20b90.trailing == _UIBackButtonMaskView:0x104c1afe0.trailing   (active)>",
    "<NSLayoutConstraint:0x282b80eb0 'Mask_Trailing_Trailing' _UIBackButtonMaskView:0x104c1afe0.trailing == _UIButtonBarButton:0x104c1d400.trailing   (active)>",
    "<NSLayoutConstraint:0x282b80ff0 'MaskEV_Leading_BIB_Trailing' H:[_UIModernBarButton:0x104c1f610]-(0)-[UIView:0x104c20b90]   (active)>",
    "<NSLayoutConstraint:0x282b813b0 'UINav_static_button_horiz_position' _UIModernBarButton:0x104c1f610.leading == UILayoutGuide:0x2831b9a40'UIViewLayoutMarginsGuide'.leading   (active)>",
    "<NSLayoutConstraint:0x282b8b7a0 'UINavItemContentGuide-leading' H:[_UIButtonBarButton:0x104c1d400]-(6)-[UILayoutGuide:0x2831b9960'UINavigationBarItemContentLayoutGuide']   (active)>",
    "<NSLayoutConstraint:0x282b89680 'UINavItemContentGuide-trailing' UILayoutGuide:0x2831b9960'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x104c1c430.trailing   (active)>",
    "<NSLayoutConstraint:0x282bfce60 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x104c1c430.width == 0   (active)>",
    "<NSLayoutConstraint:0x282b89a40 'UIView-leftMargin-guide-constraint' H:|-(8)-[UILayoutGuide:0x2831b9a40'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UINavigationBarContentView:0x104c1c430 )>"
)

Thanks in advance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source