在 SwiftUI 中填充父级

Posted

技术标签:

【中文标题】在 SwiftUI 中填充父级【英文标题】:Fill parent in SwiftUI 【发布时间】:2019-10-24 08:15:45 【问题描述】:

我有一个HStack 和两个Text,我需要每个Text 来适应它兄弟的身高,我试过.scaledToFill() 但它不起作用

struct ContentView: View 

    var body: some View 

        return HStack 
            Text("test test test test test test test test test test test test test test test test test test test test ")
                .background(Color.red)
            Text("test test test test test")
                .scaledToFill()
                .background(Color.red)
        .background(Color.blue)
    

结果:

想要的结果:

【问题讨论】:

【参考方案1】:

尝试应用相等的 .frame() 修饰符

var body: some View 
        HStack 
                Text("test test test test test test test test test test test test test test test test test test test test ")
                    .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
                    .background(Color.red)
                Text("test test test test test")
                    .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
                    .background(Color.red)
        .background(Color.blue)
    

【讨论】:

在这种情况下,视图将具有固定大小(.infinity ),但我需要文本的框架适应内容大小“test test test test....”

以上是关于在 SwiftUI 中填充父级的主要内容,如果未能解决你的问题,请参考以下文章