Hi MDeuschl,
Logically, your program should run just fine. I suspect that you are right, that it's a bug with SwiftUI itself. I have the same Fatal error: file SwiftUI, line 0 error for my code. In my case, I conditionally render a view based on whether the variable holding the view is nil. A different situation than yours, but the same error, and for no clear reason. I wish I had more information on how to avoid it. All I know for now is that you can submit a Feedback Assistant - https://feedbackassistant.apple.com/ with a sample Xcode project that demonstrates the crash. I'll probably do this when I have more time.
Here's my code. I display my view in another view that is displayed in a modal sheet. The whole modal setup works fine in a SwiftUI Preview, but always crashes on simulator and device.
swift
import SwiftUI
/// A view with an action button that should be presented modally.
struct ActionModalView: View {
let iconImage: Image?
let title: String
let message: String
let buttonText: String
let buttonAction: () - Void
var body: some View {
VStack(spacing: Theme.Spacing.extraExtraLarge) {
Spacer(minLength: Theme.Spacing.extraExtraLarge)
// This causes a crash
if let iconImage = iconImage {
iconImage
.resizable()
.renderingMode(.template)
.foregroundColor(.primaryBase)
.aspectRatio(contentMode: .fit)
.frame(width: 80)
}
Text(title)
.font(Theme.Text.largeTitle)
FadingScrollView {
Text(message)
.font(Theme.Text.title3)
}
actionButton
}
.padding(.horizontal, Theme.Spacing.extraExtraLarge)
.background(Color.adaptiveSecondaryBackground)
}
var actionButton: some View {
Button(action: buttonAction) {
HStack {
Spacer()
Text(buttonText)
.font(Theme.Text.title3)
.foregroundColor(.textXlight)
Spacer()
}
.padding()
.background(Color.primaryBase)
.cornerRadius(10)
}
}
}
struct ActionModalView_Previews: PreviewProvider {
static var previews: some View {
LightAndDarkPreviewGroup(layoutMode: .largeAndSmallDevices) {
Group {
Text("Press the play button to view the ActionModalView with a large amount of text.")
.sheet(isPresented: .constant(true)) {
ActionModalView(
iconImage: Image(uiImage: Asset.runningshoe.image),
title: "This is the title",
message: PreviewFixtures.Text.threeParagraphs,
buttonText: "Action Button",
buttonAction: {}
)
}
Text("Press the play button to view the ActionModalView with a small amount of text.")
.sheet(isPresented: .constant(true)) {
ActionModalView(
iconImage: Image(uiImage: Asset.runningshoe.image),
title: "This is the title",
message: PreviewFixtures.Text.oneParagraph,
buttonText: "Action Button",
buttonAction: {}
)
}
}
}
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: