首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用.aspectRatio(contentMode:.fill)后,文本从屏幕上消失

使用.aspectRatio(contentMode:.fill)后,文本从屏幕上消失
EN

Stack Overflow用户
提问于 2020-08-10 15:14:19
回答 2查看 294关注 0票数 1

我正在尝试使用SwiftUI制作一个带有背景图像的应用程序。然而,图像与屏幕的纵横比不同,这使得我使用.aspectRatio(contentMode: .fill)来填充整个屏幕。在我开始添加文本之前,这是完全正常的。当添加文本时,它现在会从屏幕上消失,而不是像正常情况下应该做的那样换行。

下面是我的代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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,因此非常感谢您的帮助:)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-10 15:28:23

当然可以,因为图像扩展了容器的边框,即ZStack,比屏幕宽度更宽。

这里有一个解决方案-制作图像,在真实的背景中生活自己的生活,而不影响他人。使用Xcode12/ iOS 14进行了测试。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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)
    )
}
票数 2
EN

Stack Overflow用户

发布于 2020-08-10 15:30:47

这是因为您的ZStack使用的宽度与您的图像相同,请尝试使用VStack和geometryReader包装您的图像。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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)     
}

我把这个写在我的窗口上,可能有拼写错误

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63342983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文