前提
SwiftUIで果物図鑑アプリを作っています。
ビルドが成功後、アプリの起動時にエラーメッセージが発生しました。
実現したいこと
- 本来表示されないはずのエラーを解消させて正常な画面遷移を実現させたい。
発生している問題・エラーメッセージ
2022-09-05 21:10:40.699499+0900 Fructus[51820:1190521] [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:0x6000022300f0 'BIB_Leading_Leading' H:|-(0)-[_UIModernBarButton:0x7fdb5d815a60] (active, names: '|':_UIButtonBarButton:0x7fdb5d81a880 )>", "<NSLayoutConstraint:0x600002230e60 'UINav_static_button_horiz_position' _UIModernBarButton:0x7fdb5d815a60.leading == UILayoutGuide:0x6000038095e0'UIViewLayoutMarginsGuide'.leading (active)>", "<NSLayoutConstraint:0x600002230eb0 'UINavItemContentGuide-leading' H:[_UIButtonBarButton:0x7fdb5d81a880]-(6)-[UILayoutGuide:0x6000038096c0'UINavigationBarItemContentLayoutGuide'] (active)>", "<NSLayoutConstraint:0x600002239540 'UINavItemContentGuide-trailing' UILayoutGuide:0x6000038096c0'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x7fdb5d818c30.trailing (active)>", "<NSLayoutConstraint:0x600002233110 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x7fdb5d818c30.width == 0 (active)>", "<NSLayoutConstraint:0x600002239900 'UIView-leftMargin-guide-constraint' H:|-(8)-[UILayoutGuide:0x6000038095e0'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UINavigationBarContentView:0x7fdb5d818c30 )>" )
該当のソースコード
FruitDetailView.swift
1import SwiftUI 2 3struct FruitDetailView: View { 4 // MARK: - PROPERTIES 5 6 var fruit: Fruit 7 8 // MARK: - BODY 9 10 var body: some View { 11 NavigationView { 12 ScrollView(.vertical, showsIndicators: false) { 13 VStack(alignment: .center, spacing: 20) { 14 // HEADER 15 FruitHeaderView(fruit: fruit) 16 17 VStack(alignment: .leading, spacing: 20) { 18 // TITLE 19 Text(fruit.title) 20 .font(.largeTitle) 21 .fontWeight(.heavy) 22 .foregroundColor(fruit.gradientColors[1]) 23 24 // HEADLINE 25 Text(fruit.headline) 26 .font(.headline) 27 .multilineTextAlignment(.leading) 28 29 // NUTRIENTS 30 FruitNutrientsView(fruit: fruit) 31 32 // SUBHEADLINE 33 Text("Learn more about \(fruit.title)".uppercased()) 34 .fontWeight(.bold) 35 .foregroundColor(fruit.gradientColors[1]) 36 37 // DESCRIPTION 38 Text(fruit.description) 39 .multilineTextAlignment(.leading) 40 41 // LINK 42 SourceLinkView() 43 .padding(.top, 10) 44 .padding(.bottom, 40) 45 } //: VSTACK 46 .padding(.horizontal, 20) 47 .frame(maxWidth: 640, alignment: .center) 48 } //: VSTACK 49 .navigationBarTitle(fruit.title, displayMode: .inline) 50 .navigationBarHidden(true) 51 } //: SCROLL 52 .edgesIgnoringSafeArea(.top) 53 } //: NAVIGATION 54 .navigationViewStyle(StackNavigationViewStyle()) 55 } 56} 57 58// MARK: - PREVIEW 59 60struct FruitDetailView_Previews: PreviewProvider { 61 static var previews: some View { 62 FruitDetailView(fruit: fruitsData[0]) 63 64 } 65} 66
SettingsView.swift
1import SwiftUI 2 3struct SettingsView: View { 4 // MARK: - PROPERTIES 5 6 @Environment(\.presentationMode) var presentationMode 7 8 // MARK: - BODY 9 10 var body: some View { 11 NavigationView { 12 ScrollView(.vertical, showsIndicators: false) { 13 VStack(spacing: 20) { 14 // MARK: - SECTION 1 15 16 GroupBox( 17 label: 18 SettingsLabelView(labelText: "Fructus", labelImage: "info.circle") 19 ) { 20 Divider().padding(.vertical, 4) 21 22 HStack(alignment: .center, spacing: 10) { 23 Image("logo") 24 .resizable() 25 .scaledToFit() 26 .frame(width: 80, height: 80) 27 .cornerRadius(9) 28 29 Text("Most fruits are naturally low in fat, sodium, and calories. None have cholesterol. Fruits are sources of many essential nutrients, including potassium, dietary fiber, vitamins, and much more.") 30 .font(.footnote) 31 } 32 } 33 34 // MARK: - SECTION 2 35 36 // MARK: - SECTION 3 37 38 } //: VSTACK 39 .navigationBarTitle(Text("Settings"), displayMode: .large) 40 .navigationBarItems( 41 trailing: 42 Button(action: { 43 presentationMode.wrappedValue.dismiss() 44 }) { 45 Image(systemName: "xmark") 46 } 47 ) 48 .padding() 49 } //: SCROLL 50 } //: NAVIGATION 51 .navigationViewStyle(StackNavigationViewStyle()) 52 } 53} 54 55// MARK: - PREVIEW 56 57struct SettingsView_Previews: PreviewProvider { 58 static var previews: some View { 59 SettingsView() 60 .preferredColorScheme(.dark) 61 } 62} 63
試したこと
エラーメッセージである[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
を検索し、iOS14.3から非推奨になったnavigationBarTitle を使っていた事が原因であることがわかった。
navigationView に.navigationViewStyle(StackNavigationViewStyle())を記載すればエラーが解消できましたというWebページを発見。
自分と全く同じ状況であったため、navigationBarTitleが記載されているファイルFruitDetailView.swiftとSettingsView.swiftそれぞれに.navigationViewStyle(StackNavigationViewStyle())を記載したが同じエラーが発生。→エラー解消されず。
FruitDetailView.swiftとSettingsView.swiftそれぞれのNavigationBarTitle 及び.navigationViewStyle(StackNavigationViewStyle())をコメントアウトして再ビルド。
ビルドは成功し動きとしては問題がなかったがコメントアウトをしたことによると見られる本件とは違うエラーメッセージが発生。→今回の調査対象ではないため、コメントアウトを戻した。
制約に矛盾しているから解消してねというエラーであることは間違いないようである。
https://developer.apple.com/forums/thread/673113
補足情報(FW/ツールのバージョンなど)
macOS Monterey 12.5.1
Xcode 13.4.1
Swift version 5.6.1
シミュレータ ios 15.5
回答1件
あなたの回答
tips
プレビュー