【问题标题】:SwiftUI (List & ForEach): Rotation of the device causes the current position in the List to be lostSwiftUI(List & ForEach):设备的旋转导致List中的当前位置丢失
【发布时间】:2021-03-07 19:02:28
【问题描述】:

使用下面的 SwiftUI 代码 (Xcode 12.2),在给定行的列表滚动后,设备的旋转会导致 跳转到其他行

import SwiftUI

struct ContentView: View {
    var body: some View {
        List(10...90, id: \.self) { i in
            VStack {
                ForEach(1...9, id: \.self) { j in
                    Text("item \(i).\(j)")
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

请问如何使当前行的显示对设备的旋转不敏感?

仅在滚动列表在每次旋转之间时才会出现此问题。

【问题讨论】:

    标签: list dynamic foreach swiftui rotation


    【解决方案1】:

    我认为这与列表偏移量有关,我不确定为什么。一个解决方案可能是改用 ScrollView,这似乎更具动态性。

    var body: some View {
        ScrollView {
            ForEach(10...90, id: \.self) { i in
                VStack {
                    ForEach(1...9, id: \.self) { j in
                        Text("item \(i).\(j)")
                    }
                }
                .frame(maxWidth: .infinity, alignment: .leading)
                Divider()
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2019-10-25
      • 2021-12-13
      • 2023-03-24
      相关资源
      最近更新 更多