If I run this code:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
Text("Text")
.navigationTitle("My Title")
}
}
}
I get these warnings while debugging with my iPhone:
2021-03-15 18:00:08.023556+0100 Trial2[373:7602] [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:0x283874dc0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x103817930]-(6)-[_UIModernBarButton:0x102f2e3d0'My Title'] (active)>",
"<NSLayoutConstraint:0x283874e10 'CB_Trailing_Trailing' _UIModernBarButton:0x102f2e3d0'My Title'.trailing <= BackButton.trailing (active, names: BackButton:0x102f2d9a0 )>",
"<NSLayoutConstraint:0x283875b30 'UINav_static_button_horiz_position' _UIModernBarButton:0x103817930.leading == UILayoutGuide:0x282245f80'UIViewLayoutMarginsGuide'.leading (active)>",
"<NSLayoutConstraint:0x283875b80 'UINavItemContentGuide-leading' H:[BackButton]-(0)-[UILayoutGuide:0x282245ea0'UINavigationBarItemContentLayoutGuide'] (active, names: BackButton:0x102f2d9a0 )>",
"<NSLayoutConstraint:0x283858b90 'UINavItemContentGuide-trailing' UILayoutGuide:0x282245ea0'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x102f220c0.trailing (active)>",
"<NSLayoutConstraint:0x283876300 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x102f220c0.width == 0 (active)>",
"<NSLayoutConstraint:0x283858a00 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x282245f80'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UINavigationBarContentView:0x102f220c0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x283874dc0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x103817930]-(6)-[_UIModernBarButton:0x102f2e3d0'My Title'] (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.
I've seen a similar question here, but the problem was that .navigationBarTitle
is deprecated. In my case I'm using .navigationTitle
. Is it also deprecated (I doubt it as the documentation doesn't say that)? Am I using it in a wrong way? Is it a bug of Xcode? Or simply is this normal and I shouldn't worry about these warnings?
P.S.: I'm new to Swift and SwiftUI programming
EDIT: @Andrew found something that seems to solve the issue (adding the modifier .navigationViewStyle(StackNavigationViewStyle())
to NavigationView
). Yet, I don't understand if this is the right solution for a well defined issue in my code, or a trick to workaround an Xcode false positive/bug.
I would like someone to explain me why this code seem to fix my issue.
I don't think that .navigationViewStyle(StackNavigationViewStyle())
is the right solution, because the issue happened while following the Apple's tutorial. I think that if the error were expected, Apple would know it and put this line in the tutorial. So it is easier for me to believe it is an Xcode 14 bug.
To recap, my questions are:
- Is the warning fired by Xcode expected? Or is it a bug?
- Why
.navigationViewStyle(StackNavigationViewStyle())
fixes the issue? - When is this warning really an issue? (I can't see anything wrong when I run it)