在SwiftUI中填充父级

时间:2019-10-24 08:15:45

标签: ios swift swiftui

我有一个HStack,其中有2个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)
    }
}

结果: enter image description here

所需结果: enter image description here

1 个答案:

答案 0 :(得分:0)

尝试应用相等的.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)
    }

enter image description here