简体   繁体   English

SwiftUI 列表中某些行的布局问题

[英]Problems with layout of some rows in SwiftUI list

I have tried to find a similar problem asked before, but failed.我试图找到以前问过的类似问题,但失败了。

I have a simple view with list.我有一个简单的列表视图。 I am using a ForEach to show 10 iterations of the some list item to create a layout before I will add real data to this list.我正在使用 ForEach 显示某个列表项的 10 次迭代以创建布局,然后再将实际数据添加到此列表中。 I have a problem with last 2 rows not rendering correctly.我有最后 2 行无法正确呈现的问题。 But sometimes it's other row.但有时它是另一排。 I have tested on an iPhone too and sometimes it's one row, sometimes another.我也在 iPhone 上测试过,有时是一排,有时是另一排。 The code for the view with list is this:带有列表的视图的代码是这样的:

import SwiftUI

struct LocksView: View {

    @State private var locksPaid = 0

    var body: some View {
        NavigationView {
            List {
                DateView()
                    .listRowInsets(EdgeInsets())

                Picker(selection: $locksPaid, label: Text("Picker")) {
                    Text("All").tag(0)
                    Text("Not paid (2)").tag(1)
                }
                .pickerStyle(SegmentedPickerStyle())
                .padding(10)

                ForEach(0 ..< 10) {item in
                    LocksItemView()
                }

            }
            .navigationBarTitle(Text("Locks"))
            .navigationBarItems(trailing: EditButton())
        }
    }
}

The code for list items is this:列表项的代码是这样的:

import SwiftUI

struct LocksItemView: View {

    @State private var paid : Bool = false

    var body: some View {
        HStack {

            Text("L15")
                .font(.title)
                .fontWeight(.heavy)
                .multilineTextAlignment(.center)
                .frame(width: 80)

            VStack(alignment: .leading) {
                Text("nickname")
                    .fontWeight(.bold)
                Text("category")
                Text("4 000 THB")
                    .fontWeight(.bold)
            }

            Spacer()

            Toggle(isOn: self.$paid) {
                Text("Paid")
            }
            .labelsHidden()
        }
    }
}

Why is toggle broken in some rows on my list?为什么切换在我的列表中的某些行中断开? Why it moves to the left side?为什么会向左移动?

在此处输入图像描述

It is not last items.这不是最后的项目。 If you set in your ForEach 20 instead of 10 and scroll up & down you'll see much more interesting issue.如果您将ForEach设置为 20 而不是 10 并上下滚动,您会看到更有趣的问题。

I assume the reason, actually List bug, is the same as in this topic .我假设原因,实际上是List错误,与本主题中的相同。

Workaround If it is not critical to you then use ScrollView instead of List , as tested there is no bug for it.解决方法如果它对您不重要,则使用ScrollView而不是List ,经测试,它没有错误。 Otherwise, file a Radar and wait for fix.否则,提交雷达并等待修复。

I tried your code at simulators first and had same issue too.我首先在模拟器上尝试了你的代码,也遇到了同样的问题。 But then I remembered, that there are some problems with 13.2 iOS and tried to run it on my device (iPhone 7, iOS 13.1.1) and everything works fine.但后来我记得,13.2 iOS 存在一些问题,并尝试在我的设备上运行它(iPhone 7,iOS 13.1.1),一切正常。 I think that is the problem in 13,2 iOS.我认为这是 13,2 iOS 中的问题。 not in the List, There is sample: how I changed code for demonstration that everything is ok:不在列表中,有示例:我如何更改代码以证明一切正常:

import SwiftUI

struct LocksView: View {
    
    @State private var locksPaid = 0

    var body: some View {
        NavigationView {
            List {
                Picker(selection: $locksPaid, label: Text("Picker")) {
                    Text("All").tag(0)
                    Text("Not paid (2)").tag(1)
                }
                .pickerStyle(SegmentedPickerStyle())
                .padding(10)

                ForEach(0 ..< 200) {item in
                    LocksItemView(number: item)
                }

            }
            .navigationBarTitle(Text("Locks"))
            .navigationBarItems(trailing: EditButton())
        }
    }
}

struct LocksItemView: View {

    @State private var paid : Bool = false
    var number: Int

    var body: some View {
        HStack {

            Text("L\(self.number)")
                .font(.title)
                .fontWeight(.heavy)
                .multilineTextAlignment(.center)
                .frame(width: 80)

            VStack(alignment: .leading) {
                Text("nickname")
                    .fontWeight(.bold)
                Text("category")
                Text("4 000 THB")
                    .fontWeight(.bold)
            }

            Spacer()

            Toggle(isOn: self.$paid) {
                Text("Paid")
            }
            .labelsHidden()
        }
    }
}

and on my phone the result is:在我的手机上,结果是:

在此处输入图像描述

so there are bugs in 13.2 version and I hope Apple will fix them all所以在 13.2 版本中存在错误,我希望 Apple 将它们全部修复

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

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