mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-04-27 02:05:48 +00:00
53 lines
1.5 KiB
Swift
53 lines
1.5 KiB
Swift
//
|
|
// StickyScrollView.swift
|
|
// SimpleX (iOS)
|
|
//
|
|
// Created by user on 20/09/2024.
|
|
// Copyright © 2024 SimpleX Chat. All rights reserved.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
struct StickyScrollView<Content: View>: UIViewRepresentable {
|
|
@ViewBuilder let content: () -> Content
|
|
|
|
func makeUIView(context: Context) -> UIScrollView {
|
|
let hc = context.coordinator.hostingController
|
|
hc.view.backgroundColor = .clear
|
|
let sv = UIScrollView()
|
|
sv.showsHorizontalScrollIndicator = false
|
|
sv.addSubview(hc.view)
|
|
sv.delegate = context.coordinator
|
|
return sv
|
|
}
|
|
|
|
func updateUIView(_ scrollView: UIScrollView, context: Context) {
|
|
let hc = context.coordinator.hostingController
|
|
hc.rootView = content()
|
|
hc.view.frame.size = hc.view.intrinsicContentSize
|
|
scrollView.contentSize = hc.view.intrinsicContentSize
|
|
}
|
|
|
|
func makeCoordinator() -> Coordinator {
|
|
Coordinator(content: content())
|
|
}
|
|
|
|
class Coordinator: NSObject, UIScrollViewDelegate {
|
|
let hostingController: UIHostingController<Content>
|
|
|
|
init(content: Content) {
|
|
self.hostingController = UIHostingController(rootView: content)
|
|
}
|
|
|
|
func scrollViewWillEndDragging(
|
|
_ scrollView: UIScrollView,
|
|
withVelocity velocity: CGPoint,
|
|
targetContentOffset: UnsafeMutablePointer<CGPoint>
|
|
) {
|
|
if targetContentOffset.pointee.x < 32 {
|
|
targetContentOffset.pointee.x = 0
|
|
}
|
|
}
|
|
}
|
|
}
|