英文:
Vertical Paging UIScrollView incorrect height
问题
I understand your request. Here's the translation of your code and issue:
我正在尝试实现类似Instagram Reels UI的吸附滚动视图。内容使用SwiftUI创建,滚动视图是UIScrollView
包装在UIViewRepresentable
中。我启用了UIScrollView
上的分页以获得滚动时的吸附到项功能。
滚动视图的内容只是一些我设置为屏幕全宽和全高的文本:
滚动视图的表示如下:
然后,我在主结构中使用ignoreSafeArea
实例化了ScrollSnapView
:
正如你所看到的,当我滚动到第二页时,顶部会显示一点红色。当我滚动到第三页时,会显示一点绿色。如何使UIScrollView
分页到每一页的确切边界?
我尝试过在makeUIView
中删除这行代码:
它在分页时包括了安全区域边界(这不是我想要的,我想完全隐藏安全区域),但至少它完全滚动到每一页的顶部边界。
我尝试过这个答案,但不起作用。
英文:
I'm trying to implement a snapping scroll view like that of an Instagram Reels UI. The contents are in SwiftUI, and the scroll view is a UIScrollView
wrapped in UIViewRepresentable
. I enable paging on the UIScrollView
to get the snap to item on scroll feature.
The contents of the scroll view is just some Texts that I set to full width and height of the screen:
struct FeedView: View {
var body: some View {
VStack(spacing: 0) {
Text("")
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.background(.red)
Text("")
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.background(.green)
Text("")
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.background(.blue)
}
}
}
The scroll view representable is as follows:
struct ScrollSnapView: UIViewRepresentable {
func makeUIView(context: Context) -> UIScrollView {
let view = UIScrollView()
view.isPagingEnabled = true
view.showsVerticalScrollIndicator = false
view.showsHorizontalScrollIndicator = false
view.contentInsetAdjustmentBehavior = .never
let child = UIHostingController(rootView: FeedView())
child.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * 3)
view.contentSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * 3)
view.addSubview(child.view)
return view
}
func updateUIView(_ uiView: UIScrollView, context: Context) {
}
}
And then I instantiate a ScrollSnapView
with ignoreSafeArea
in the main struct:
@main
struct App: App {
var body: some Scene {
WindowGroup {
ScrollSnapView()
.ignoresSafeArea()
}
}
}
Here is a recording of the scrolling:
As you can see, when I scroll to the second page, there is a little red visible on the top. And when I scroll to the third page, there is a little green visible. How can I make UIScrollView
page to the exact bounds of each page?
I've tried removing this line in makeUIView
:
view.contentInsetAdjustmentBehavior = .never
It includes the safe area bounds when paging (which I don't want, I want to completely hide the safe area), but at least it fully scrolls to the top bound of each page.
I've tried this answer, but it doesn't work.
答案1
得分: 3
只需在VStack
中添加.ignoresSafeArea()
。
struct FeedView: View {
var body: some View {
VStack(spacing: 0) {
Text("")
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.background(.red)
Text("")
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.background(.green)
Text("")
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.background(.blue)
}.ignoresSafeArea() //注意这一行
}
}
英文:
Just add .ignoresSafeArea()
in the VStack
struct FeedView: View {
var body: some View {
VStack(spacing: 0) {
Text("")
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.background(.red)
Text("")
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.background(.green)
Text("")
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.background(.blue)
}.ignoresSafeArea() //notice this line
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论