垂直分页的UIScrollView高度不正确

huangapple go评论66阅读模式
英文:

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:

垂直分页的UIScrollView高度不正确

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
  }
}

huangapple
  • 本文由 发表于 2023年5月25日 03:20:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定