V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ChrisFreeMan
V2EX  ›  Swift

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

  •  
  •   ChrisFreeMan · 2022-04-08 15:24:13 +08:00 · 1324 次点击
    这是一个创建于 970 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我在使用 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])
        }
    }
    
    2 条回复    2022-04-15 12:37:19 +08:00
    chipmuck
        1
    chipmuck  
       2022-04-15 11:20:54 +08:00   ❤️ 2
    给 NavigationView 添加一个 .navigationViewStyle(.stack) 的 modifier 试试看。
    ChrisFreeMan
        2
    ChrisFreeMan  
    OP
       2022-04-15 12:37:19 +08:00
    @chipmuck 起作用了,多谢兄弟,强迫症给你治好了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1396 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 17:40 · PVG 01:40 · LAX 09:40 · JFK 12:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.