最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swiftui - How to constrain width of cells inside a Grid inside a ScrollView? - Stack Overflow

programmeradmin5浏览0评论

I want to achieve the following. I have a datastructure Table, which is basically a 2D array of Strings. I want to display them with the following constraints: Each column must be the width of the widest cell in that column, with a maximum of 300. If the text doesn't fit, the text should wrap. This is my best attempt:

ScrollView(.horizontal, showsIndicators: false) {
    Grid(horizontalSpacing: 20, verticalSpacing: 10) {
        ForEach(table.rows) { row in
            GridRow {
                ForEach(row.cells) { cell in
                    DocumentContentView(content: cell)
                        .frame(maxWidth: 300)
                }
            }
        }
    }
}
.scrollBounceBehavior(.basedOnSize, axes: .horizontal)
.scrollClipDisabled()

However, this truncates the text. If I remove the scrollview, it works as expected, but then the columns become way too narrow on iPhone. I want the table to be scrollable. Does anyone have an idea?

发布评论

评论列表(0)

  1. 暂无评论