【问题标题】:Unable to simultaneously satisfy constraints. SwiftUI无法同时满足约束。斯威夫特用户界面
【发布时间】:2023-02-23 04:55:56
【问题描述】:

我不确定为什么会遇到此错误,因为我的任何链接视图上都没有任何视图修饰符,但我却遇到此错误:

[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:0x6000003807d0 'accessoryView.bottom' _UIRemoteKeyboardPlaceholderView:0x7fc40b6609d0.bottom == _UIKBCompatInputView:0x7fc40b577c70.top   (active)>",
    "<NSLayoutConstraint:0x6000003be350 'assistantHeight' SystemInputAssistantView.height == 45   (active, names: SystemInputAssistantView:0x7fc40b5050a0 )>",
    "<NSLayoutConstraint:0x600000380aa0 'assistantView.bottom' SystemInputAssistantView.bottom == _UIKBCompatInputView:0x7fc40b577c70.top   (active, names: SystemInputAssistantView:0x7fc40b5050a0 )>",
    "<NSLayoutConstraint:0x600000380af0 'assistantView.top' V:[_UIRemoteKeyboardPlaceholderView:0x7fc40b6609d0]-(0)-[SystemInputAssistantView]   (active, names: SystemInputAssistantView:0x7fc40b5050a0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600000380af0 'assistantView.top' V:[_UIRemoteKeyboardPlaceholderView:0x7fc40b6609d0]-(0)-[SystemInputAssistantView]   (active, names: SystemInputAssistantView:0x7fc40b5050a0 )>

我的 SwiftUI 视图如下:

struct RecordView: View {
    @EnvironmentObject var modelView : JournalRecordsModelView
    
    @State private var navigationPath: [JournalRecordsModel.Record] = []
    
    @State private var showAddRecord: Bool = false
    
    var body: some View {
        NavigationStack(path: $navigationPath) {
            List {
                ForEach(modelView.currentData) { record in
                    NavigationLink(value: record, label: { Text(record.timeDate) })
                }.onDelete(perform: { index in
                    index.forEach({ i in
                        modelView.deleteRecord(i)
                    })
                })
            }
            .navigationDestination(for: JournalRecordsModel.Record.self) { record in
                RecordDetailedView(record: record, navigationPath: $navigationPath).environmentObject(modelView)
            }
            .navigationTitle("Your Records")
            .navigationBarItems(trailing: Button(action: {
                showAddRecord.toggle()
            }, label: {
                Image(systemName: "plus")
            }))
            .sheet(isPresented: $showAddRecord) {
                AddRecordView(showAddRecord: self.$showAddRecord).environmentObject(modelView)
            }
        }
    }
}
struct AddRecordView: View {
    @EnvironmentObject var modelView : JournalRecordsModelView
    @Binding var showAddRecord: Bool
    @State private var showSubmitAddAlert: Bool = false
    @State private var dateTime = Date.now
    @State private var title: String = ""
    @State private var content: String = ""
    @State private var feeling: String = "Nil"
    var body: some View {
        HStack {
            Text("Add Record")
                .font(.title)
                .fontWeight(.bold)
                .frame(
                    width: UIScreen.main.bounds.width / 2.2,
                    height: 20,
                    alignment: .leading
                ).padding([.leading])
            Button(action : {
                showAddRecord.toggle()
            },
                   label: {
                Image(systemName: "xmark")
            }).frame(
                width: UIScreen.main.bounds.width / 2.2,
                height: 20,
                alignment: .trailing
             )
            .padding([.trailing])
        }.padding([.top, .bottom])
        
        DatePicker("Date and Time", selection: $dateTime)
            .padding(.horizontal)
        
        TextField("Entry Name", text: $title)
            .padding(.horizontal)
        
        TextField("What are your thoughts today?", text: $content)
            .padding(.horizontal)
        
        Text("How are you feeling?")
            .font(.body)
            .padding(.horizontal)
        
        Picker("How do you feel?", selection: $feeling) {
            ForEach(modelView.currentFeelings, id: \.self) { feeling in
                Text(feeling)
            }
        }
        .padding(.horizontal)
        .pickerStyle(MenuPickerStyle())
        
        Spacer()
        Button {
            modelView.addRecord(dateTime, title, content, feeling)
            showSubmitAddAlert.toggle()
        }
        label: {
                Image(systemName: "doc.fill.badge.plus")
        }
        .disabled(title.isEmpty || content.isEmpty || feeling == "Nil")
        .alert("Record added. Please confirm addition of record.", isPresented: $showSubmitAddAlert) {
            //the moment i click OK on the alert, have constraints error, button is causing the error
            **Button("Ok", role: .cancel) {
                showAddRecord.toggle()
            }**
        }
    }
}

AddRecordView 中的按钮似乎是导致问题的原因,但我不确定为什么会导致问题。即使弹出错误,用户界面和应用程序在运行时也不会崩溃。如果有任何建议,我将不胜感激。谢谢。

我已经检查了所有变量名称并检查了与视图大小相关的视图的任何修饰符,因为我假设错误与尺寸有关。

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    您看到的消息更多是作为错误的提示/警告。有些约束确实会发生冲突,正如消息告诉您的那样,它将通过打破一个约束来解决这个问题。这不是什么大问题,但可能会导致 UI 看起来不像您想要的那样。无论如何你应该检查 layoutConstraint 系统以了解这里发生了什么。列出的约束是系统约束,因此很难通过检查来弄清楚。 你可以尝试的是:

    **Button("Ok", role: .cancel) {
       showSubmitAddAlert = false
       showAddRecord = false
    }**
    

    这应该在关闭呈现的 AddRecordView 之前关闭警报 此外,我建议将 bool 显式设置为 true/false,因为在每种情况下都不应该出现相反切换的情况(使其更易于阅读,并且如果未将值设置为也可以防止不需要的行为它应该是)。

    【讨论】:

      猜你喜欢
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      • 1970-01-01
      • 2014-10-27
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多