首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SwiftUI - ScrollView和TextEditor高度不起作用

SwiftUI - ScrollView和TextEditor高度不起作用
EN

Stack Overflow用户
提问于 2022-01-28 18:29:30
回答 1查看 703关注 0票数 0

我将再次发布这个问题,并提供我的代码和视图的简化视图。我会删除旧的,因为我认为我没有正确地解释我的问题(而且它还没有答案)。

我试图定位一个初始高度在屏幕底部的HStack。顶部是滚动视图。随着文本编辑器中键入更多文本,HStack可能会扩展到一定程度。

我能够做到这一点,只要没有文本编辑器。

我需要帮助找出如何用文本编辑器.来做这件事

请见下文-

只有一个文本视图

使用文本编辑器而不是文本视图

这是密码-

代码语言:javascript
运行
复制
struct ContentView: View {

@State var myText: String = "This Text Editor is screaming \n\n THIS IS SPARTA!!! \n\n at me and kicking me into the abyss of similarly worded Stackoverflow questions."

var body: some View{
    
    VStack(spacing:0){
        
        GeometryReader {geo in
            ScrollView(.vertical, showsIndicators: false){
                ForEach(1 ..< 200, id: \.self){ num in
                    Text("\(num)")
                        .frame(width: geo.size.width)
                        .padding(5)
                        .background(.yellow)
                }
            }
            .background(.orange)
            .frame(minWidth: geo.size.width, maxHeight: geo.size.height * 0.96 , alignment: .top)
        }
        
        HStack(spacing: 0){
            Text("This HStack is supposed to contain a text editor that expands as needed to a max height but starts off at this height.")
                .padding()
                .background(.teal)
            
            /*TextEditor(text: $myText)
             .multilineTextAlignment(.center)
             .font(.title3)
             .padding()*/
        }
        .frame(minHeight:50)
        .background(.teal)
    }
}}

任何在正确的方向上的帮助都是非常感谢的!

EN

回答 1

Stack Overflow用户

发布于 2022-01-28 19:06:46

见以下附评论的内容:

代码语言:javascript
运行
复制
struct ContentView: View {

    @State var myText: String = "This Text Editor is screaming \n\n THIS IS SPARTA!!! \n\n at me and kicking me into the abyss of similarly worded Stackoverflow questions."

    var body: some View{
        
        VStack(spacing: 0) {
                ScrollView(.vertical, showsIndicators: false) {
                    ForEach(1 ..< 200, id: \.self) { num in
                        Text("\(num)")
                            // The GeometryReader is unnecessary. Use a frame with .infinity to expand your row.
                            .frame(maxWidth: .infinity)
                            .background(.yellow)
                            .padding(5)
                    }
                }
                .background(.orange)
            
            HStack(spacing: 0) {
                TextEditor(text: $myText)
                 .multilineTextAlignment(.center)
                 .font(.title3)
                 // Use .fixedSize to reduce the height of the TextEditor to its minimal height.
                 .fixedSize(horizontal: false, vertical: true)
                 // this .padding() gives you space around your TextEditor for margins
                 .padding()
                 // this background gives you white margins
                 .background(Color.white)
                 // this .padding() keeps the background above from resizing to the size of the teal background.
                 .padding()
            }
            .frame(minHeight: 50)
            .background(.teal)
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70898593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档