使ScrollView跨越屏幕的整个宽度
我正在尝试使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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然,SwiftUI似乎将
.infinity
的固定大小解释为此特定帧修饰符中的默认大小行为。要实现所需的目标,请使用
.frame(minwidth:0,maxWidth:.infinity)
内在text> text
元素的修改器使它们尽可能宽,并且直至scrollview
的容器将相应地调整大小,因此您不需要.frame
修改器scrollview
: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 innerText
elements to make them as wide as possible, and the containers all the way up toScrollView
will resize accordingly, so you won't need the.frame
modifier for theScrollView
: