【发布时间】:2021-04-05 23:09:48
【问题描述】:
下面是一个在 VStack 中具有 Text 和 TextField 的应用的简约示例:
import SwiftUI
struct ContentView: View {
@State private var textEntry : String = "Hello World"
var body: some View {
return VStack {
Text(textEntry)
TextField("Enter new text here", text: $textEntry)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
应用程序运行良好,即 Text 小部件显示您在 TextField 小部件中键入的内容。问题是您会收到无法满足布局约束的错误(或警告):
2020-12-29 10:31:13.800514+0100 SwiftuiTest[32286:2781544] [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.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x600001d50f00 h=--& v=--& _UIButtonBarButton:0x7fba5550cee0.height == 0 (active)>",
"<NSLayoutConstraint:0x600001d4e7b0 _UIUCBKBSelectionBackground:0x7fba5550da00.bottom == _UIButtonBarButton:0x7fba5550cee0.bottom - 6 (active)>",
"<NSLayoutConstraint:0x600001d4e710 V:|-(6)-[_UIUCBKBSelectionBackground:0x7fba5550da00] (active, names: '|':_UIButtonBarButton:0x7fba5550cee0 )>"
)
在这里使用堆栈是错误的吗?基本上我只想拥有一些 Button、Text、TextField 小部件,它们使用显示器上的可用高度...
更新:
macOS 11.1 Xcode 12.3 (12C33)
iOS 部署目标 14.3 iOS 模拟器:例如iPad(第 8 代)
$ xcodebuild -showsdks
iOS SDKs:
iOS 14.3 -sdk iphoneos14.3
iOS Simulator SDKs:
Simulator - iOS 14.3 -sdk iphonesimulator14.3
macOS SDKs:
DriverKit 20.2 -sdk driverkit.macosx20.2
macOS 11.1 -sdk macosx11.1
tvOS SDKs:
tvOS 14.3 -sdk appletvos14.3
tvOS Simulator SDKs:
Simulator - tvOS 14.3 -sdk appletvsimulator14.3
watchOS SDKs:
watchOS 7.2 -sdk watchos7.2
watchOS Simulator SDKs:
Simulator - watchOS 7.2 -sdk watchsimulator7.2
项目是在 Xcode 中创建的,具有界面 SwiftUI 和生命周期 SwiftUI App。这会创建两个 Swift 源文件,例如 SimpleTestApp.swift 和 ContentView.swift:
- 文件
ContentView.swift已修改如上图 - 文件
SimpleTestApp.swift未修改,内容如下:
import SwiftUI
@main
struct SimpleTestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
【问题讨论】:
-
不可重现。在这种情况下,您只需提供发生这种情况的操作系统版本、SDK 和环境。
-
很公平。我添加了 OS 版本、Xcode 版本和 sdk 版本
-
布局异常发生在您无法控制的私有元素中,位于 SwiftUI 下方的 UIKit 层中。这种情况有时会发生,除非它影响 UI 的呈现,否则可以忽略。烦人,但不是你可以控制的。
-
控制台中出现和消失的此类警告从版本到版本...并表明 SwiftUI 引擎内部存在缺陷。如果您喜欢向 Apple 报告反馈。