我正在尝试使用 SwiftUI 制作一个带有背景图像的应用程序。然而,图像的宽高比与屏幕不同,这使得我习惯用.aspectRatio(contentMode: .fill)它来填充整个屏幕。在我开始添加文本之前,这工作完全正常。添加文本时,它现在会离开屏幕,而不是像通常那样换行。
这是我的代码:
\nstruct FeaturesView: View\n{\n var body: some View\n {\n ZStack\n {\n Image("background")\n .resizable()\n .aspectRatio(contentMode: .fill)\n .edgesIgnoringSafeArea(.all)\n \n VStack(alignment: .leading)\n {\n Text("Hello this is some sample text that i am writing to show that this text goes off the screen.")\n }\n .foregroundColor(.white)\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n这是预览:
\n\n正如您所看到的,文本从屏幕上消失了。我尝试使用 \xc2\xb4.frame()\xc2\xb4 并指定宽度和高度来修复它,但这会在其他视图中使用视图时导致问题。我正在使用 Xcode 12 beta。
\n我是 Swift 和 SwiftUI 的新手,因此感谢所有帮助:)
\n