swiftui - SwiftUI 无法同时满足约束
问题描述
我是 6 年以上的 android 开发人员,但最近我开始做 iOS。我选择了 SwiftUI,我认为它很棒,并构建了一些屏幕。
但是,我在日志中以及第一次单击 TextField 时收到此错误。当我这样做时,屏幕将返回上一个屏幕(我将从 a 转到此屏幕navigationLink
)
我遇到的错误是这样的:
2021-01-10 12:52:58.682264+0200 appName[2025:1085202] [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:0x280e898b0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x11d57ed20]-(6)-[_UIModernBarButton:0x11d57e3e0'Add Point'] (active)>",
"<NSLayoutConstraint:0x280e89900 'CB_Trailing_Trailing' _UIModernBarButton:0x11d57e3e0'Add Point'.trailing <= _UIButtonBarButton:0x11d57e010.trailing (active)>",
"<NSLayoutConstraint:0x280e8a440 'UINav_static_button_horiz_position' _UIModernBarButton:0x11d57ed20.leading == UILayoutGuide:0x28143ed80'UIViewLayoutMarginsGuide'.leading (active)>",
"<NSLayoutConstraint:0x280e8a490 'UINavItemContentGuide-leading' H:[_UIButtonBarButton:0x11d57e010]-(0)-[UILayoutGuide:0x28143eca0'UINavigationBarItemContentLayoutGuide'] (active)>",
"<NSLayoutConstraint:0x280e436b0 'UINavItemContentGuide-trailing' UILayoutGuide:0x28143eca0'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x11d543390.trailing (active)>",
"<NSLayoutConstraint:0x280e8ab70 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x11d543390.width == 0 (active)>",
"<NSLayoutConstraint:0x280e43d40 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x28143ed80'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UINavigationBarContentView:0x11d543390 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x280e898b0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x11d57ed20]-(6)-[_UIModernBarButton:0x11d57e3e0'Add Point'] (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.
我的观点是这样的:
import SwiftUI
struct AddEvent: View {
var body: some View {
NavigationView {
VStack {
TextField("Title", text: $title)
.font(Font.system(size: 16, weight: .bold, design: .serif))
.padding(10)
.overlay(RoundedRectangle(cornerRadius: 10).stroke(Color("AccentColor"), lineWidth: 1))
TextEditor(text: $details)
.font(Font.system(size: 15, weight: .bold, design: .serif))
.overlay(RoundedRectangle(cornerRadius: 10).stroke(Color("AccentColor"), lineWidth: 1))
.frame(maxWidth: .infinity, minHeight: 100, maxHeight: 150, alignment: .top)
DatePicker("Date and time", selection: $dateTime)
.padding()
Button(action: {
self.showingDetail.toggle()
}) {
Text("Pick coordonates")
}.sheet(isPresented: $showingDetail) {
Capsule()
.fill(Color.secondary)
.frame(width: 60, height: 4)
.padding(16)
AdminMapsView(isLongClickEnabled: true, hideLocationButton: false, latitude: self.$latitude, longitude: self.$longitude)
.padding(.top, 32)
}
.padding()
Button(action: {
if self.latitude > 0.0 && self.longitude > 0.0 && !self.titlu.isEmpty && !self.detaliu.isEmpty {
databaseService.addEvent(title: self.titlu, details: self.detaliu, date: self.dateTime, lat: self.latitude, long: self.longitude)
self.showingAlert = true
self.titlu = ""
self.detaliu = ""
} else {
self.mandatory = true
}
}) {
Text("Add event")
.frame(maxWidth: .infinity)
.padding(10.0)
.overlay(
RoundedRectangle(cornerRadius: 10.0)
.stroke(lineWidth: 2.0)
)
}.padding(.top, 15)
.alert(isPresented: $showingAlert) {
Alert(title: Text("Felicitări"), message: Text("Mesajul a fost adăugat cu succes."), dismissButton: .default(Text("Închidere")))
}
.alert(isPresented: $mandatory) {
Alert(title: Text("Atenție"), message: Text("Toate campurile trebuie sa fie completate."), dismissButton: .default(Text("Închidere")))
}
Spacer()
}
.navigationBarTitle("Add Point")
.navigationBarHidden(true)
.padding()
}
}
}
struct AddEvent_Previews: PreviewProvider {
static var previews: some View {
AddEvent()
}
}
我删除了一些变量,因此代码将更少且更容易理解。
我发现调试该日志非常困难,如果有人可以给我一些提示,我认为苹果开发人员文档非常模棱两可且难以遵循。谢谢!
解决方案
推荐阅读
- r - R中的moran.test
- java - 我在哪里可以找到实现 org.osgi.service.log.LoggerFactory 服务的包?
- apache-spark - “spark.yarn.executor.memoryOverhead”和“spark.memory.offHeap.size”之间的区别
- javascript - 根据数据属性范围显示 li 元素
- python - Turtle 代码不起作用,显示 py-lint no member 错误
- python - 随机时间延迟非整数
- python - 在 def 范围内使用 pandas 函数是否有问题
- php - Laravel 无法连接到 XAMPP MySQL 数据库(Windows)
- python - 将 tf.Dataset 提供给 fit() 时出错:KeyError: 'embedding_input'
- html - 父显示:flex 不适用于子 div?