在 SwiftUI 的列表中包含 TextEditor 的动态行高 [英] Dynamic row hight containing TextEditor inside a List in SwiftUI

查看:27
本文介绍了在 SwiftUI 的列表中包含 TextEditor 的动态行高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 List 包含一个 TextEditor

I have a List containing a TextEditor

struct ContentView: View {
    @State var text: String = "test"

    var body: some View {
        List((1...10), id: \.self) { _ in
            TextEditor(text: $text)
        }
    }
}

但是它的项目没有随着 TextEditor 的高度变化而增长.我试过 .fixedSize() 修饰符但没有成功.我在这里错过了什么?

But it's items are not growing on height change of the TextEditor. I have tried .fixedSize() modifier with no luck. What am I missing here?

推荐答案

您可以在 ZStack 中使用不可见的 Text 使其动态化.

You can use an invisible Text in a ZStack to make it dynamic.

struct ContentView: View {
    @State var text: String = "test"

    var body: some View {
        List((1...10), id: \.self) { _ in
            ZStack {
                TextEditor(text: $text)
                Text(text).opacity(0).padding(.all, 8) // <- This will solve the issue if it is in the same ZStack
            }
        }
    }
}

请注意,您应该考虑更改字体大小和其他属性以匹配 TextEditor

这篇关于在 SwiftUI 的列表中包含 TextEditor 的动态行高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆