[英]SwiftUI - ScrollView and TextEditor Height Not Working
我再次发布这个问题,简化了我的代码和视图。 我将删除旧的,因为我认为我没有在那里正确解释我的问题(而且它还没有答案)。
我正在尝试 position 一个初始高度位于屏幕底部的 HStack。 顶部是滚动视图。 随着在文本编辑器中输入更多文本,HStack 可能会扩展到一定程度。
只要没有文本编辑器,我就能做到这一点。
我需要帮助弄清楚如何使用文本编辑器执行此操作。
请看下文——
这是它的代码 -
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)
}
}}
非常感谢任何正确方向的帮助!
请参阅以下注释:
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.