[英]How to fill SwiftUI's VStack from bottom to top with items
I have been searching but could not find a way.我一直在寻找,但找不到方法。
I would like to fill VStack view from bottom to top.我想从下到上填充 VStack 视图。 When a Text view added as a child to VStack for example it is places at the top.例如,当将 Text 视图作为子项添加到 VStack 时,它位于顶部。 I wanted to add it to the bottom of screen and second one on top of the first one.我想将它添加到屏幕底部,第二个添加到第一个之上。
@State var items: [Int] = [0]
var body: some View {
HStack {
Button(action: {
items.append( items.count + 1)
}){
Text("Add")
}
ScrollView() {
ForEach(0..<items.count, id: \.self) { index in
VStack(spacing:5) {
Text(String(items[index]))
}
}
}
}
}
What you get on top of screen;您在屏幕上看到的内容;
1 1
2 2
What I want at the bottom of screen;我在屏幕底部想要什么;
2 2
1 1
You can achieve it by applying rotationEffect
to your ScrollView
and its inner content.您可以通过将rotationEffect
应用于您的ScrollView
及其内部内容来实现它。
@State var items: [Int] = [0]
var body: some View {
HStack {
Button(action: {
//Insert Items at 0 position to add content from bottom to top side
items.insert(items.count, at: 0)
}){
Text("Add")
}
ScrollView(showsIndicators: false) {
ForEach(0..<items.count, id: \.self) { index in
Text(String(items[index])).padding(.vertical,5)
}.rotationEffect(Angle(degrees: 180)) //After rotating scrollview, now rotate your inner content to upright position.
}.rotationEffect(Angle(degrees: -180)) //Rotate scrollview to bottom side
}
}
How's this?这个怎么样?
ScrollView {
VStack(spacing: 5) {
Spacer()
ForEach(items.map(String.init).reversed(), id: \.self) { item in
Text(item)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.