ScrollView中有两个适合文本内容的文本

时间:2020-09-06 22:13:24

标签: swiftui swiftui-text swiftui-scrollview

我在Text中有两个VStack。两种文本的行数都可以无限增加。我希望如果文本的大小超出屏幕的大小,我可以滚动并查看所有内容。

第一种方法:

var body: some View {
    VStack(alignment: .center, spacing: 0) {

        VStack(alignment: .leading, spacing: 8) {
            Text(title)
                .font(.title)
            Text(subtitle)
                .font(.subheadline)
        }
            .frame(maxWidth: .infinity)
            .background(Color.orange)
            .padding(.top, 24)
            .padding(.horizontal, 24)

        Button(buttonTitle) { }
            .frame(maxWidth: .infinity, minHeight: 50)
            .background(Color.red)
            .foregroundColor(.white)
            .padding(.all, 24)

    }.background(Color.gray)
        .frame(maxWidth: UIScreen.main.bounds.size.width - 20)
}

结果:

enter image description here

当然没有滚动...所以我添加了ScrollView:

var body: some View {
    VStack(alignment: .center, spacing: 0) {

        ScrollView {
            VStack(alignment: .leading, spacing: 8) {
                Text(title)
                    .font(.title)
                Text(subtitle)
                    .font(.subheadline)
            }
                .frame(maxWidth: .infinity)
                .background(Color.orange)
                .padding(.top, 24)
                .padding(.horizontal, 24)
        }

        Button(buttonTitle) { }
            .frame(maxWidth: .infinity, minHeight: 50)
            .background(Color.red)
            .foregroundColor(.white)
            .padding(.all, 24)

    }.background(Color.gray)
        .frame(maxWidth: UIScreen.main.bounds.size.width - 20)
}

结果:

enter image description here

有效!但是当文本不足以填充屏幕的整个高度时,ScrollView仍会扩展到可用的整个空间,我希望它包装内容并“禁用”滚动功能。

我用很少的文字所得到的示例:

enter image description here

我想用很少的文字得到的样本:

enter image description here

所以问题是如何使ScrollView来“包装”内容,而不是扩展到父View的整个高度?

0 个答案:

没有答案