我将再次发布这个问题,并提供我的代码和视图的简化视图。我会删除旧的,因为我认为我没有正确地解释我的问题(而且它还没有答案)。
我试图定位一个初始高度在屏幕底部的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)
}
}}
任何在正确的方向上的帮助都是非常感谢的!
发布于 2022-01-28 19:06:46
见以下附评论的内容:
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)
}
}
}
https://stackoverflow.com/questions/70898593
复制相似问题