无法同时满足约束,SwiftUI

anauzrmj  于 2022-12-21  发布在  Swift
关注(0)|答案(1)|浏览(124)

我是Swift和SwiftUI的新手,我不确定为什么会遇到这个错误,因为我的任何链接视图上都没有任何视图修改器,但我还是遇到了这个错误:

[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视图如下:
x一个一个一个一个x一个一个二个x
AddRecordView中的按钮似乎是导致问题的原因,但我不确定它导致问题的原因。UI和应用程序在运行时不会崩溃,即使错误弹出。我将感谢任何建议提前。谢谢。
我已经检查了所有变量名,并检查了与视图大小相关的视图的所有修改器,因为我假设错误与尺寸有关。

s4n0splo

s4n0splo1#

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

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

这应该在关闭当前的AddRecordView之前关闭警报。另外,我建议将布尔值显式设置为true/false,因为在任何情况下都不应该出现相反的情况(使其更易于阅读,并且如果值没有设置为应有的值,也可以防止不必要的行为)。

相关问题