SwiftUI でナビゲーションバーを使った実装をしていると、実行時に LayoutConstraints の warning が表示されるようになっていました。
SwiftUISample[38246:1524076] [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:0x600001194aa0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x7ffbc1d1b0b0]-(6)-[_UIModernBarButton:0x7ffbc1f18140'SwiftUI\U5b9f\U88c5\U30b5\U30f3\U30d7\U30eb'] (active)>",
"<NSLayoutConstraint:0x600001194af0 'CB_Trailing_Trailing' _UIModernBarButton:0x7ffbc1f18140'SwiftUI\U5b9f\U88c5\U30b5\U30f3\U30d7\U30eb'.trailing <= _UIButtonBarButton:0x7ffbc1f178b0.trailing (active)>",
"<NSLayoutConstraint:0x6000011958b0 'UINav_static_button_horiz_position' _UIModernBarButton:0x7ffbc1d1b0b0.leading == UILayoutGuide:0x600000ba0e00'UIViewLayoutMarginsGuide'.leading (active)>",
"<NSLayoutConstraint:0x600001195900 'UINavItemContentGuide-leading' H:[_UIButtonBarButton:0x7ffbc1f178b0]-(0)-[UILayoutGuide:0x600000ba0d20'UINavigationBarItemContentLayoutGuide'] (active)>",
"<NSLayoutConstraint:0x60000119cd70 'UINavItemContentGuide-trailing' UILayoutGuide:0x600000ba0d20'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x7ffbc1f10990.trailing (active)>",
"<NSLayoutConstraint:0x600001196080 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x7ffbc1f10990.width == 0 (active)>",
"<NSLayoutConstraint:0x60000119d130 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x600000ba0e00'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UINavigationBarContentView:0x7ffbc1f10990 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600001194aa0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x7ffbc1d1b0b0]-(6)-[_UIModernBarButton:0x7ffbc1f18140'SwiftUI実装サンプル'] (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.
ソースコードとしてはこんな感じ。
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
Text("Hello World!")
}
.navigationBarTitle("SwiftUI実装サンプル", displayMode: .inline)
}
}
}
調べてみるとこちらは現在非推奨になっているようでした。
navigationBarItems(leading:trailing:) | Apple Developer Documentation
Sets the navigation bar items for this view.
新たにできた実装方法だと toolbar を使うようですが、こちらは iOS14 以降で利用可能なため、iOS13 をサポートする場合は使うことができません。
追記:toolbar についても記事を書きました。
とはいえ warning が出たままなのはなんとも気持ち悪いため調べたところ、どうやら .navigationViewStyle(StackNavigationViewStyle())
を追記すれば大丈夫なようでした。
SwiftUI NavigationView navigationBarTitle LayoutConstraints issue
This is maybe related to the new (12.3) version of XCode that came out recently but I have a very simple SwiftUI View: import SwiftUI struct HomeView: View { ...
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
Text("Hello World!")
}
.navigationBarTitle("SwiftUI実装サンプル", displayMode: .inline)
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
これで warning が消えたので、とりあえずよしとします。