簡體   English   中英

如何用項目從下到上填充 SwiftUI 的 VStack

[英]How to fill SwiftUI's VStack from bottom to top with items

我一直在尋找,但找不到方法。

我想從下到上填充 VStack 視圖。 例如,當將 Text 視圖作為子項添加到 VStack 時,它位於頂部。 我想將它添加到屏幕底部,第二個添加到第一個之上。

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

您在屏幕上看到的內容;

1

2

我在屏幕底部想要什么;

2

1

您可以通過將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
    }
}

這個怎么樣?

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM