Last active
June 7, 2021 23:36
-
-
Save kylebshr/118cc4909745e00340422305e88a53db to your computer and use it in GitHub Desktop.
UISheetPresentationController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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