gpt4 book ai didi

ios - 可快速滚动全屏 View

转载 作者:行者123 更新时间:2023-11-29 05:11:08 28 4
gpt4 key购买 nike

我希望在 Swift UI 中创建全屏可 ScrollView 。将 View 放入 ScrollView 和垂直堆栈后,我在使 View 全屏显示时遇到问题吗?有什么建议么?

这就是我的代码的样子

struct ContentView : View {
var body: some View {

//var posts = [Post(postColor: .red), Post(postColor: .blue), Post(postColor: .green)]
ScrollView(.vertical, showsIndicators: false) {

VStack {
Post(postColor: .red)
Post(postColor: .blue)
Post(postColor: .green)

}
}
}

}

struct Post : View {

var postColor : Color

var body : some View {

VStack(alignment: .leading) {

HStack {
Text("Name")
.font(.title)
.fontWeight(.heavy)

Spacer()
}

Text("@Username")
.lineLimit(nil)
.font(.body)


Spacer()
}.background(postColor)

}

}

图片 This is the output I have after embedding in a scrollview.

图片 I wish to have a full screen view like this but with the ability to scroll down to the next view(basically scroll down to next screen)

最佳答案

您可以使用GeometryReader来实现此目的:

struct FullScreenViewsInScrollView: View {
var body: some View {

GeometryReader { geometry in

ScrollView {

VStack(spacing: 0) {
Rectangle()
.foregroundColor(.red)
.frame(height: geometry.size.height)

Rectangle()
.foregroundColor(.blue)
.frame(height: geometry.size.height)

Rectangle()
.foregroundColor(.green)
.frame(height: geometry.size.height)
}

}

}
}

}

结果应该是(我在实时预览中向下滚动了一点):

enter image description here

关于ios - 可快速滚动全屏 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59674291/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com