【问题标题】:SwiftUI: Text + TextField in VStack results in "Unable to simultaneously satisfy constraints."SwiftUI:VStack 中的 Text + TextField 导致“无法同时满足约束”。
【发布时间】: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.swiftContentView.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 报告反馈。

标签: ios swiftui


【解决方案1】:

如果您查看日志中提到的类型 _UIButtonBarButton_UIUCBKBSelectionBackground,您会发现布局异常发生在您无法控制的私有元素中,位于 SwiftUI 下方的 UIKit 层中。前面有_ 的名字是一个很大的赠品。

这种情况有时会发生,除非它影响 UI 的呈现,否则可以忽略。烦人,但不是你可以控制的。从这个角度来看,您可以认为这是一个警告。

【讨论】:

    【解决方案2】:

    这是 iOS 14 中引入的 TextView 和 TextField 中的错误,不仅影响 SwiftUI 组件,还影响基于故事板的 UI 组件。目前我们将不得不忽略它。

    【讨论】:

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