SwiftUI 终端 debug 信息[LayoutConstraints] Unable to simultaneously satisfy constraints,求助

2022-04-08 15:24:13 +08:00
 ChrisFreeMan

我在使用 NavigationView 这个组件的时候抛出了以下信息,要如何排查,我也是第一次写 swift ,完全不知所云,网上虽然也有人遇到了差不多的问题,但是每个人的情况都不一样。

2022-04-08 15:08:39.897732+0800 Memorize[59861:7906093] [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:0x6000036c65d0 'BIB_Leading_Leading' H:|-(0)-[_UIModernBarButton:0x15b71cb40]   (active, names: '|':_UIButtonBarButton:0x15b71a0b0 )>",
    "<NSLayoutConstraint:0x6000036c7340 'UINav_static_button_horiz_position' _UIModernBarButton:0x15b71cb40.leading == UILayoutGuide:0x600002ce6f40'UIViewLayoutMarginsGuide'.leading   (active)>",
    "<NSLayoutConstraint:0x6000036c7390 'UINavItemContentGuide-leading' H:[_UIButtonBarButton:0x15b71a0b0]-(6)-[UILayoutGuide:0x600002ce6e60'UINavigationBarItemContentLayoutGuide']   (active)>",
    "<NSLayoutConstraint:0x6000036d8230 'UINavItemContentGuide-trailing' UILayoutGuide:0x600002ce6e60'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x15b714790.trailing   (active)>",
    "<NSLayoutConstraint:0x6000036d9f90 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x15b714790.width == 0   (active)>",
    "<NSLayoutConstraint:0x6000036d85f0 'UIView-leftMargin-guide-constraint' H:|-(8)-[UILayoutGuide:0x600002ce6f40'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UINavigationBarContentView:0x15b714790 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000036c65d0 'BIB_Leading_Leading' H:|-(0)-[_UIModernBarButton:0x15b71cb40]   (active, names: '|':_UIButtonBarButton:0x15b71a0b0 )>

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.

我把 swiftUI 官方的教程也跑了下,也是会抛出这些错误,我现在暂时根据 stackoverflow 的回答关闭了这些信息,想问下各位这个可不可以解决

关闭的代码是

UserDefaults.standard.setValue(false, forKey:"_UIConstraintBasedLayoutLogUnsatisfiable")

项目中抛出错误的代码是

struct MemoryGameList: View {
    var body: some View {
        NavigationView{
            List{
                ForEach(MemoryGameHandler.emojiDatas){ emoji in
                    NavigationLink{
                        EmojiMemoryGameView(
                            emojiGame: MemoryGameHandler(with: emoji.theme),
                            theme: emoji.theme
                        )
                    } label: {
                        MemoryGameRow(emoji: emoji)
                    }
                    .tag(emoji)
                }
            }
            .navigationTitle("Game List")
            Text("Select a Game")
        }
    }
}

struct MemoryGameRow: View {
    var emoji: EmojiData
    
    var body: some View {
        HStack{
            Text(emoji.theme.rawValue)
            Text(datasSubList.joined())
        }
        .padding(.vertical, 4)
    }
    
    var datasSubList: [String]{
        Array(emoji.datas[...10])
    }
}

struct MemoryGameRow_Previews: PreviewProvider {
    static var previews: some View {
        MemoryGameRow(emoji: MemoryGameHandler.emojiDatas[0])
    }
}
1096 次点击
所在节点    Swift
2 条回复
chipmuck
2022-04-15 11:20:54 +08:00
给 NavigationView 添加一个 .navigationViewStyle(.stack) 的 modifier 试试看。
ChrisFreeMan
2022-04-15 12:37:19 +08:00
@chipmuck 起作用了,多谢兄弟,强迫症给你治好了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/845719

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX