我正在try 实现一个类似于Instagram Reels用户界面的抓拍滚动视图.内容在SwiftUI中,滚动视图是一个包含在UIViewRepresentable中的UIScrollView.我在UIScrollView上启用了分页功能,以获得滚动时对齐项目功能.

滚动视图的内容只是我设置为屏幕全宽和全高的一些文本:

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

可表示的滚动视图如下:

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

然后我用主 struct 中的ignoreSafeArea实例化一个ScrollSnapView:

@main
struct App: App {
    var body: some Scene {
        WindowGroup {
            ScrollSnapView()
                .ignoresSafeArea()
        }
    }
}

以下是滚动的录音:

enter image description here

如你所见,当我滚动到第二页时,在顶部可以看到一个小红色.当我滚动到第三页时,可以看到一个小绿色.我怎么才能把UIScrollView页精确到每页的边界呢?

我试着在makeUIView页中删除这一行:

view.contentInsetAdjustmentBehavior = .never

它包括分页时的安全区域边界(我不想要,我想完全隐藏安全区域),但至少它完全滚动到每一页的顶部边界.

我试了this个答案,但不起作用.

推荐答案

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()  //notice this line
  }
}

Ios相关问答推荐

自定义alert 未执行任何操作

iOS中的分段拾取器—手柄点击已 Select 的项目

如何在SwiftUI中扩展双击的可检测区域?

如何让3张组合的`SF Symbol`图片围绕一个特定点旋转?

UIAlertController的空子类是否可以与`appearance(whenContainedInInstancesOf:)`一起安全使用

如何防止UITest套件与Fastlane一起执行?

Xcode 15模拟器用x86_64编译

swiftui动画如何实时确定不对称过渡

多协议和类继承混乱,Swift

使用 CIImage 支持的 UIImage 设置 UIImageView 时发生罕见的崩溃

通过MatchedGeometryEffect向上滑动视图

如何将私钥添加到分发证书?

iOS 中是否有用于自定义振动的 API?

Xcode 故事板:内部错误.请提交错误

SRCROOT 和 PROJECT_DIR 有什么区别?

在 iOS 上存储身份验证令牌 - NSUserDefaults 与 keys 串?

如何确定 WKWebView 的内容大小?

打开 XIB 文件后 Xcode 6.3 冻结/挂起

如何检测快速touch 或单击的tableView单元格

iOS13状态栏背景 colored颜色 与大文本模式下的导航栏不同