我正在尝试使用SwiftUI制作一个带有背景图像的应用程序。然而,图像与屏幕的纵横比不同,这使得我使用.aspectRatio(contentMode: .fill)
来填充整个屏幕。在我开始添加文本之前,这是完全正常的。当添加文本时,它现在会从屏幕上消失,而不是像正常情况下应该做的那样换行。
下面是我的代码:
struct FeaturesView: View
{
var body: some View
{
ZStack
{
Image("background")
.resizable()
.aspectRatio(contentMode: .fill)
.edgesIgnoringSafeArea(.all)
VStack(alignment: .leading)
{
Text("Hello this is some sample text that i am writing to show that this text goes off the screen.")
}
.foregroundColor(.white)
}
}
}
这是预览:
如您所见,文本从屏幕上消失了。我尝试过使用´.frame()并指定宽度和高度来修复它,但在其他视图中使用该视图时会出现问题。我使用的是Xcode12测试版。
我不熟悉Swift和SwiftUI,因此非常感谢您的帮助:)
发布于 2020-08-10 15:28:23
当然可以,因为图像扩展了容器的边框,即ZStack,比屏幕宽度更宽。
这里有一个解决方案-制作图像,在真实的背景中生活自己的生活,而不影响他人。使用Xcode12/ iOS 14进行了测试。
var body: some View
{
ZStack
{
// ... other content
VStack(alignment: .leading)
{
Text("Hello this is some sample text that i am writing to show that this text goes off the screen.")
}
.foregroundColor(.white)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(
Image("background")
.resizable()
.aspectRatio(contentMode: .fill)
.edgesIgnoringSafeArea(.all)
)
}
发布于 2020-08-10 15:30:47
这是因为您的ZStack使用的宽度与您的图像相同,请尝试使用VStack和geometryReader
包装您的图像。
GeometryReader { geometry in
ZStack {
Image() // your image
VStack(alignment: .leading) {
Text("Hello this is some sample text that i am writing to show that this text goes off the screen.")
}.foregroundColor(.white)
}.frame(maxWidth: geometry.size.width)
}
我把这个写在我的窗口上,可能有拼写错误
https://stackoverflow.com/questions/63342983
复制相似问题