【问题描述】
MacOS: Big Sur 11.1 (20C69)
XCode Version: Version 12.3 (12C33)
今天更新了xcode后,重新运行之前写的代码,抛出[LayoutConstraints]Unable to simulataneously satisfy constraints
的异常。
具体异常如下:
2020-12-16 22:37:50.113721+0800 Blog[14493:431069] [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:0x6000033ce8f0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x7febabc16310]-(6)-[_UIModernBarButton:0x7febabd0b680'Posts'] (active)>",
"<NSLayoutConstraint:0x6000033ce940 'CB_Trailing_Trailing' _UIModernBarButton:0x7febabd0b680'Posts'.trailing <= _UIButtonBarButton:0x7febabe132a0.trailing (active)>",
"<NSLayoutConstraint:0x6000033f2490 'UINav_static_button_horiz_position' _UIModernBarButton:0x7febabc16310.leading == UILayoutGuide:0x6000029cebc0'UIViewLayoutMarginsGuide'.leading (active)>",
"<NSLayoutConstraint:0x6000033f24e0 'UINavItemContentGuide-leading' H:[_UIButtonBarButton:0x7febabe132a0]-(0)-[UILayoutGuide:0x6000029d02a0'UINavigationBarItemContentLayoutGuide'] (active)>",
"<NSLayoutConstraint:0x6000033e3f70 'UINavItemContentGuide-trailing' UILayoutGuide:0x6000029d02a0'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x7febabf0bc50.trailing (active)>",
"<NSLayoutConstraint:0x6000033cad50 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x7febabf0bc50.width == 0 (active)>",
"<NSLayoutConstraint:0x6000033c8280 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x6000029cebc0'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UINavigationBarContentView:0x7febabf0bc50 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000033ce8f0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x7febabc16310]-(6)-[_UIModernBarButton:0x7febabd0b680'Posts'] (active)>
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.
根据上述异常信息,从UINavigationBarItemContentLayouGuide
得知抛出异常点为导航栏相关地方。删除导航栏相关配置,即可正常运行。后定位到具体异常为方法.navigationTitle("Posts")
。
相关代码如下:
var body: some View {
NavigationView{
VStack {
List{
ForEach(postListVM.posts, id: \.postId){ post in
NavigationLink(
destination: PostDetailView(post: post),
label: {
Text(post.title)
})
}
.onDelete(perform: { indexSet in
self.deleetPost(at: indexSet)
})
}
.onAppear(){
self.postListVM.fetchAllPosts()
}
.sheet(isPresented: $isPresented, onDismiss: {
self.postListVM.fetchAllPosts()
}, content: {
AddPostView()
})
}
// 问题方法
.navigationTitle("Posts")
.navigationBarItems(trailing: Button("Add Post"){
self.isPresented = true
})
}
}
【解决方案】
删除此行代码。。。
待更新。。。