Skip to content

Instantly share code, notes, and snippets.

@kylebshr
Last active June 7, 2021 23:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylebshr/118cc4909745e00340422305e88a53db to your computer and use it in GitHub Desktop.
Save kylebshr/118cc4909745e00340422305e88a53db to your computer and use it in GitHub Desktop.
UISheetPresentationController
//
// ViewController.swift
// Sheets
//
// Created by Kyle Bashour on 6/7/21.
//
import UIKit
class ViewController: UIViewController, UISheetPresentationControllerDelegate {
var sheetPresentationController: UISheetPresentationController {
presentationController as! UISheetPresentationController
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
let presentButton = makeButton(title: "Present Sheet") {
let viewController = ViewController()
self.present(viewController, animated: true)
}
let grabberButton = makeButton(title: "Toggle Grabber") {
self.sheetPresentationController.animateChanges {
self.sheetPresentationController.prefersGrabberVisible.toggle()
}
}
let detentButton = makeButton(title: "Toggle Detent") {
self.sheetPresentationController.animateChanges {
if self.sheetPresentationController.selectedDetentIdentifier == .medium {
self.sheetPresentationController.selectedDetentIdentifier = .large
} else {
self.sheetPresentationController.selectedDetentIdentifier = .medium
}
}
}
let cornerRadiusLabel = UILabel()
cornerRadiusLabel.text = "Corner Radius"
let cornerRadiusSlider = UISlider(frame: .zero, primaryAction: .init { action in
let slider = action.sender as! UISlider
let value = slider.value
self.sheetPresentationController.animateChanges {
// Not public (yet?)
self.sheetPresentationController.setValue(CGFloat(value), forKey: "preferredCornerRadius")
}
})
cornerRadiusSlider.minimumValue = 0
cornerRadiusSlider.maximumValue = 100
let stack = UIStackView(arrangedSubviews: [
presentButton,
grabberButton,
detentButton,
cornerRadiusLabel,
cornerRadiusSlider,
UIView()
])
stack.spacing = 20
stack.axis = .vertical
stack.translatesAutoresizingMaskIntoConstraints = false
stack.alignment = .center
stack.distribution = .fill
view.addSubview(stack)
NSLayoutConstraint.activate([
stack.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor, constant: 80),
stack.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor),
stack.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor),
stack.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),
cornerRadiusSlider.widthAnchor.constraint(equalTo: stack.widthAnchor),
])
sheetPresentationController.delegate = self
sheetPresentationController.selectedDetentIdentifier = .large
sheetPresentationController.prefersGrabberVisible = true
sheetPresentationController.detents = [
.medium(),
.large(),
]
}
private func makeButton(title: String, handler: @escaping () -> Void) -> UIButton {
let button = UIButton(configuration: .tinted(), primaryAction: .init { _ in
handler()
})
button.setTitle(title, for: .normal)
return button
}
func sheetPresentationControllerDidChangeSelectedDetentIdentifier(_ sheetPresentationController: UISheetPresentationController) {
// Doesn't seem to work
print("Detent changed")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment