简体   繁体   中英

SwiftUI: Text + TextField in VStack results in “Unable to simultaneously satisfy constraints.”

Here some minimalistic example for an app that has a Text and a TextField within a VStack:

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()
    }
}

The app works fine, ie the Text widget displays what you type into the TextField widget. The problem is that you get an error (or warning) that the layout constrains can not be satisfied:

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 )>"
)

Is it wrong to use a Stack here? Basically I just want to have a few Button, Text, TextField widgets that use the available height on the display...

Update:

macOS 11.1 Xcode 12.3 (12C33)

iOS deployment target 14.3 iOS Simulator: eg iPad (8th generation)

$ 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

Project was created in Xcode with interface SwiftUI and life cycle SwiftUI App . This creates two Swift source files, eg SimpleTestApp.swift and ContentView.swift :

  • File ContentView.swift was modified as shown above
  • File SimpleTestApp.swift was left unmodified and has the following content:
import SwiftUI

@main
struct SimpleTestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

If you look at the types referred to in the log _UIButtonBarButton and _UIUCBKBSelectionBackground you can see that the layout exception is happening in private elements you don't control, down in the UIKit layer below SwiftUI. The names having a _ in front is a big giveaway.

This happens sometimes and unless it's affecting the rendering of your UI, can be ignored. Annoying but not something you can control. From that perspective you can consider this to be a warning.

This is a bug in TextView and TextField introduced in iOS 14 and affects not only SwiftUI components but also storyboard based UI components. At the moment we'll have to ignore it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM