繁体   English   中英

SwiftUI (List & ForEach):设备旋转导致列表中的当前位置丢失

[英]SwiftUI (List & ForEach): Rotation of the device causes the current position in the List to be lost

使用下面的 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 偏移量有关,我不确定为什么。 一个解决方案可能是使用 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()
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM