【问题标题】:How to make List reversed in SwiftUI如何在 SwiftUI 中使列表反转
【发布时间】:2020-02-18 23:19:54
【问题描述】:

我是 SwiftUI 的新手,正在尝试在 Android LinearLayoutManager 中制作类似 reverse 的东西

messagesRecyclerView = view.findViewById(R.id.messagesRecyclerView);

manager = new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, true); // => true means reverse layout
messagesRecyclerView.setLayoutManager(manager);

在这种情况下,一切都是从下到上的。

我能找到的所有有用的东西都是tableview 反向: Load tableview from bottom, scroll up (reverse tableview) (iOS)

//In ViewDidLoad
conversationTableView.transform = CGAffineTransform(rotationAngle: -(CGFloat)(Double.pi));

//In cellForRowAtIndexPath
cell.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi));

但我不知道如何将其添加到 List 中(似乎它只是 TableView 的包装)。 这里的另一种方法:How to populate UITableView from the bottom upwards?

                List {
                    ForEach(itemsData.messages) { item in
                        ChatRow(item: item)
                    }
                    Text("Load more messages...")
                        .onAppear() {
                            print("Load more messages...")
                            self.itemsData.next()
                    }
                }

我的假设是获得一些.onAppearoverride 方法来完成这项工作。

似乎 SwiftUI 还太年轻,无法深入。

另一个问题:他们有 API 以编程方式在 List 中滚动

希望我没有重复任何问题。

将其用于匿名聊天应用https://en.lonje.com/

【问题讨论】:

  • itemsData.messages.reversed() 呢?
  • @Jorge 这是我开始的地方,但是: * 它没有对齐 * 更新时总是跳到顶部 * 总是在任何 messages 更改时重置位置
  • 发现这篇很棒的文章,正是我所要求的。 stackoverflow.com/questions/57258846/…

标签: ios swift swiftui swiftui-list


【解决方案1】:

@cipais 的回答效果很好。 https://stackoverflow.com/a/61036551/12208004

List(chatController.messages, id: \.self) { message in
    MessageView(message.text, message.isMe)
        .rotationEffect(.radians(.pi))
        .scaleEffect(x: -1, y: 1, anchor: .center)
}
.rotationEffect(.radians(.pi))
.scaleEffect(x: -1, y: 1, anchor: .center)

【讨论】:

    【解决方案2】:

    UITableView 有一个技巧,您可以翻转表格和每个单元格。所以它似乎是从底部填充的。你也可以在 SwiftUI 中做到这一点:

    完整的工作演示:

    struct ContentView: View {
        @State private var ids = [String]()
    
        init() {
            UITableView.appearance().tableFooterView = UIView()
            UITableView.appearance().separatorStyle = .none
        }
    
        var body: some View {
            ZStack {
                List(ids, id: \.self) { id in
                    Text(id).scaleEffect(x: 1, y: -1, anchor: .center)
                }.scaleEffect(x: 1, y: -1, anchor: .center)
    
                Button("Touch") {
                    self.ids.append(UUID().uuidString)
                }
            }
        }
    }
    

    【讨论】:

    • 请注意我将所有数据替换为缺少有关您的结构信息的模拟二重奏。
    • 谢谢,这正是我一直在寻找的。但在我改进的示例中reversed List 有奇怪的padding,它看起来像一些列表“标题”或其他东西,无法摆脱它。这是我的 sn-p:pastebin.com/rRGtewzG
    • 您不应使用答案更新您的问题。请将其恢复为原始版本并询问有关该有线填充的新问题并将其链接到此处的评论中。所以我可以调查一下;)
    • 已更新,感谢您的快速回复。 stackoverflow.com/questions/58516228/… 顺便说一句,您可以推荐一些用于在列表中进行编程滚动的示例,例如如何scrollToPosition(at: i)
    • 目前还没有直接的api,但我会寻找解决方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2011-04-25
    • 2021-07-28
    相关资源
    最近更新 更多