使ScrollView跨越屏幕的整个宽度

发布于 2025-01-31 10:14:07 字数 567 浏览 6 评论 0原文

我正在尝试使ScrollView的宽度跨越整个屏幕,但是尽管使用了无穷大,但由于某种原因,宽度并没有跨越整个屏幕。

ScrollView {
    VStack(spacing: 20) {
        ForEach(0..<10) {
            Text("Item \($0)")
                .foregroundColor(.white)
                .font(.largeTitle)
                .frame(width: .infinity, height: 200.0)
                .background(.red)
        }
    }
}
.frame(width : .infinity , height: 750)

当前的scrollview

I am trying to make the width of the scrollview span the entire screen, however the width for some reason does not span the entire screen despite using infinity.

ScrollView {
    VStack(spacing: 20) {
        ForEach(0..<10) {
            Text("Item \($0)")
                .foregroundColor(.white)
                .font(.largeTitle)
                .frame(width: .infinity, height: 200.0)
                .background(.red)
        }
    }
}
.frame(width : .infinity , height: 750)

Current Scrollview

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛徒 2025-02-07 10:14:07

显然,SwiftUI似乎将.infinity的固定大小解释为此特定帧修饰符中的默认大小行为。

要实现所需的目标,请使用.frame(minwidth:0,maxWidth:.infinity)内在text> text元素的修改器使它们尽可能宽,并且直至scrollview的容器将相应地调整大小,因此您不需要.frame修改器scrollview

ScrollView {
    VStack(spacing: 20) {
        ForEach(0..<10) {
            Text("Item \($0)")
                .foregroundColor(.white)
                .font(.largeTitle)
                .frame(minWidth: 0, maxWidth: .infinity)
                .background(.red)
        }
    }
}

Apparently, SwiftUI seems to interpret the fixed size of .infinity as the default sizing behavior in this particular frame modifier.

To achieve what you need, use the .frame(minWidth: 0, maxWidth: .infinity) modifier for the inner Text elements to make them as wide as possible, and the containers all the way up to ScrollView will resize accordingly, so you won't need the .frame modifier for the ScrollView:

ScrollView {
    VStack(spacing: 20) {
        ForEach(0..<10) {
            Text("Item \($0)")
                .foregroundColor(.white)
                .font(.largeTitle)
                .frame(minWidth: 0, maxWidth: .infinity)
                .background(.red)
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文