簡體   English   中英

在 SwiftUI 應用程序中按下按鈕時滾動到特定行的列表

[英]List that scrolls to certain row when button pressed in SwiftUI app

在 SwiftUI 應用程序中,我試圖創建一個在按下按鈕時自動滾動到下一行的列表。

Apparently, this functionality isn't currently possible with a SwiftUI List, but is with UITableView using the scrollToRowAtIndexPath method as mentioned in part #3 of this answer https://stackoverflow.com/a/58830883/11698443 This answer is well explained but沒有建議如何實施他們所說的最佳解決方案。

下面是一些示例代碼,說明列表在 SwiftUI 中的外觀。 目標是按下按鈕,然后列表滾動到下一行。 我認為該解決方案需要與 UIKit 接口才能使其工作。 如果有人能弄清楚如何以最干凈的方式做到這一點,那就太棒了。 不幸的是,這不能使用所有 SwiftUI 來完成。

提前感謝任何認為他們可以解決這個問題的人。

import SwiftUI

struct ScrollingList: View {
    var body: some View {
        List(0 ..< 10) { item in
            VStack {
                Text("\(item)")

                Button(action: {}) {
                Text("Scroll To Next Item")
                }
            }
            .frame(height: 500)
            .background(Color.blue)
        }
    }
}

查看這個漂亮的庫https://github.com/siteline/SwiftUI-Introspect 有了它,你可以這樣做:

import Introspect

List {
  Text("some")
  Text("list")
}.introspectTableView { tableView in
          tableView.scrollToRowAtIndexPath(<#IndexPath#>, atScrollPosition: <#UITableView.ScrollPosition#>, animated: <#Bool#>)
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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