Skip to content

Instantly share code, notes, and snippets.

@auramagi
Created July 10, 2025 00:01
Show Gist options
  • Save auramagi/295fb8d649e6b4f10aa62f2ee1ae2102 to your computer and use it in GitHub Desktop.
Save auramagi/295fb8d649e6b4f10aa62f2ee1ae2102 to your computer and use it in GitHub Desktop.
UIScrollEdgeElementContainerInteraction test
import SwiftUI
struct ContentView: View {
var body: some View {
// 1: Not nested in NC
ListVCRepresentable()
.ignoresSafeArea()
// 2: Nested in NC
// NavigationStack {
// ListVCRepresentable()
// .ignoresSafeArea()
// .navigationTitle("Home")
// .navigationBarTitleDisplayMode(.inline)
// }
}
}
struct ListVCRepresentable: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> some UIViewController {
ListVC(collectionViewLayout: UICollectionViewCompositionalLayout.list(using: .init(appearance: .insetGrouped)))
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
}
}
final class ListVC: UICollectionViewController {
@ViewLoading var dataSource: UICollectionViewDiffableDataSource<Int, Int>
override func viewDidLoad() {
super.viewDidLoad()
let registration = UICollectionView.CellRegistration<UICollectionViewListCell, Int> { cell, indexPath, item in
cell.contentConfiguration = UIHostingConfiguration {
Color.purple.frame(width: 100, height: 100)
}
}
dataSource = .init(collectionView: collectionView) { collectionView, indexPath, itemIdentifier in
collectionView.dequeueConfiguredReusableCell(using: registration, for: indexPath, item: itemIdentifier)
}
var snapshot = NSDiffableDataSourceSnapshot<Int, Int>()
snapshot.appendSections([0])
snapshot.appendItems((0...100).map { $0 })
dataSource.apply(snapshot)
collectionView.topEdgeEffect.style = .hard
let header = UIView()
let interaction = UIScrollEdgeElementContainerInteraction()
interaction.edge = .top
interaction.scrollView = collectionView
header.addInteraction(interaction)
var buttonConfig = UIButton.Configuration.borderedProminent()
buttonConfig.title = "Button"
let button = UIButton(configuration: buttonConfig)
view.addSubview(header)
header.addSubview(button)
header.translatesAutoresizingMaskIntoConstraints = false
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
header.leadingAnchor.constraint(equalTo: view.leadingAnchor),
header.trailingAnchor.constraint(equalTo: view.trailingAnchor),
header.topAnchor.constraint(equalTo: view.topAnchor),
button.topAnchor.constraint(equalTo: header.safeAreaLayoutGuide.topAnchor),
button.leadingAnchor.constraint(equalTo: header.leadingAnchor, constant: 16),
header.bottomAnchor.constraint(equalTo: button.bottomAnchor, constant: 16),
])
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment