Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.navigationTitle aways creates an autolayout-constraint conflict

Environment: Xcode Version 12.3 (12C33)
Version 12.3 (12C33)

I notice that the NavigationTitle breaks the autolayout after updating to Xcode 12.3 RC.
Here's a simple example (revised to modern syntax per comment):

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack(alignment: .center, spacing: 10) {
                Text("Hello, world!")
                    .padding()
            }.navigationTitle(Text("Greetings World!"))
            .navigationBarTitleDisplayMode(.inline)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Here's what I get upon running this:

2020-12-09 11:42:19.994389-0800 UICheck[26092:799713] [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:0x60000170e1c0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x7feb5ac096e0]-(6)-[_UIModernBarButton:0x7feb5ac06db0'Greetings World!']   (active)>",
    "<NSLayoutConstraint:0x60000170e210 'CB_Trailing_Trailing' _UIModernBarButton:0x7feb5ac06db0'Greetings World!'.trailing <= _UIButtonBarButton:0x7feb5ac066a0.trailing   (active)>",
    "<NSLayoutConstraint:0x60000170ef80 'UINav_static_button_horiz_position' _UIModernBarButton:0x7feb5ac096e0.leading == UILayoutGuide:0x600000d5db20'UIViewLayoutMarginsGuide'.leading   (active)>",
    "<NSLayoutConstraint:0x60000170efd0 'UINavItemContentGuide-leading' H:[_UIButtonBarButton:0x7feb5ac066a0]-(0)-[UILayoutGuide:0x600000d5da40'UINavigationBarItemContentLayoutGuide']   (active)>",
    "<NSLayoutConstraint:0x60000170ae90 'UINavItemContentGuide-trailing' UILayoutGuide:0x600000d5da40'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x7feb5ae185f0.trailing   (active)>",
    "<NSLayoutConstraint:0x60000170f750 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x7feb5ae185f0.width == 0   (active)>",
    "<NSLayoutConstraint:0x60000170b250 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x600000d5db20'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UINavigationBarContentView:0x7feb5ae185f0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60000170e1c0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x7feb5ac096e0]-(6)-[_UIModernBarButton:0x7feb5ac06db0'Greetings World!']   (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.

This is the source of the problem:

enter image description here

I suspect this to be another Apple Bug.
Being that I'm using the supplied .navigationBarTitle view, how can I fix this?

like image 995
Frederick C. Lee Avatar asked Dec 09 '20 19:12

Frederick C. Lee


People also ask

What are constraints in Swift?

Auto Layout constraints allow us to create views that dynamically adjust to different size classes and positions. The constraints will make sure that your views adjust to any size changes without having to manually update frames or positions.

How to Add constraints in uikit?

To create a constraint between two views, Control-click one of the views and drag to the other. When you release the mouse, Interface Builder displays a HUD menu with a list of possible constraints.

How to write auto layout constraints in code?

Writing Auto Layout constraints in code 1 Writing constraints by using Layout Anchors. First of all, we need to set the translatesAutoresizingMaskIntoConstraints to false. ... 2 Order of constraints. ... 3 Available layout guides. ... 4 Supporting Right-to-Left languages. ... 5 Performance of dynamically enabling and disabling constraints. ...

Why is stacknavigationviewstyle still causing a Contraint error?

This is still causing a contraint error unless you add the StackNavigationViewStyle modifier. However, the documentation for StackNavigationViewStyle states: Do not use this type directly.

Is navigationbartitle deprecated in Xcode?

It had nothing to do with NavigationBarTitle. . navigationTitle is not deprecated. Seems whole issue is mainly with new Xcode 12.4 update, even for simple code: adding .navigationViewStyle (StackNavigationViewStyle ()) fixes problem PS. My first stackoverflow reply xD Show activity on this post.

When should I override the viewwilllayoutsubviews and layoutsubviews methods?

The resulting layouts are more robust and easier to debug. You should only override the viewWillLayoutSubviews or layoutSubviews methods when you need to create a layout that cannot be expressed with constraints alone. When overriding these methods, the layout is in an inconsistent state.


Video Answer


1 Answers

Updated for Xcode 13

To solve the LayoutConstraints error on SwiftUI NavigationView, make it conform to this style:

.navigationViewStyle(StackNavigationViewStyle()) on Xcode 12

.navigationViewStyle(.stack) on Xcode 13

struct ContentView: View {

  var body: some View {
    NavigationView {
      VStack(alignment: .center, spacing: 10) {
        Text("Hello world!")
          .padding()
      }
      .navigationTitle("Greetings World!")
      .navigationBarTitleDisplayMode(.inline)
    }
    .navigationViewStyle(.stack)
  }
}
like image 139
Roland Lariotte Avatar answered Oct 23 '22 05:10

Roland Lariotte