简体   繁体   English

SwiftUI - ScrollView 和 TextEditor 高度不起作用

[英]SwiftUI - ScrollView and TextEditor Height Not Working

I'm posting this question again, with a simplified view of my code and views.我再次发布这个问题,简化了我的代码和视图。 I'll delete the old one since I think I didn't explain my issue properly there (and it has no answers yet).我将删除旧的,因为我认为我没有在那里正确解释我的问题(而且它还没有答案)。

I'm trying to position a HStack with an initial height at the bottom of the screen.我正在尝试 position 一个初始高度位于屏幕底部的 HStack。 With the top portion being a scroll view.顶部是滚动视图。 The HStack might expand to a certain extent as more text is typed into the Text Editor.随着在文本编辑器中输入更多文本,HStack 可能会扩展到一定程度。

I am able to achieve this, as long as there is no Text Editor.只要没有文本编辑器,我就能做到这一点。

I need help with figuring out how to do this with Text Editor.我需要帮助弄清楚如何使用文本编辑器执行此操作。

Please see below -请看下文——

With just a text view只有一个文本视图

With a text editor instead of text view使用文本编辑器而不是文本视图

Here's the code for it -这是它的代码 -

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)
    }
}}

Any help in the right direction is greatly appreciated!非常感谢任何正确方向的帮助!

See the following with comments:请参阅以下注释:

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)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM