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, i.e. 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: e.g. 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, e.g SimpleTestApp.swift and ContentView.swift:
- File
ContentView.swiftwas modified as shown above - File
SimpleTestApp.swiftwas left unmodified and has the following content:
import SwiftUI
@main
struct SimpleTestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}