Skip to content

Instantly share code, notes, and snippets.

@RaajeevChandran
Last active April 8, 2025 10:54
Show Gist options
  • Select an option

  • Save RaajeevChandran/234303dc302201b05c9952420857469b to your computer and use it in GitHub Desktop.

Select an option

Save RaajeevChandran/234303dc302201b05c9952420857469b to your computer and use it in GitHub Desktop.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let btn = UIButton(type: .system)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setTitle("Show Sheet", for: .normal)
btn.addTarget(self, action: #selector(tap), for: .touchUpInside)
view.addSubview(btn)
NSLayoutConstraint.activate([
btn.centerXAnchor.constraint(equalTo: view.centerXAnchor),
btn.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
}
@objc func tap() {
let bottomSheetVC = OurBottomSheetContentViewController()
let delegate = BottomSheetTransitioningDelegate()
bottomSheetVC.transitioningDelegate = delegate
bottomSheetVC.modalPresentationStyle = .custom
present(bottomSheetVC, animated: true, completion: nil)
}
}
class OurBottomSheetContentViewController: UIViewController {
let contentView = ContentView()
override func loadView() {
super.loadView()
self.view = contentView
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
}
}
class ContentView: UIView {
let titleLabel = UILabel()
let subtitleLabel = UILabel()
let okButton = UIButton()
init() {
super.init(frame: .zero)
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.font = .systemFont(ofSize: 22, weight: .bold)
titleLabel.text = "What's up?"
subtitleLabel.translatesAutoresizingMaskIntoConstraints = false
subtitleLabel.font = .systemFont(ofSize: 18, weight: .medium)
subtitleLabel.text = "This is a custom bottom sheet that we made from scratch!"
subtitleLabel.numberOfLines = 0
subtitleLabel.textAlignment = .center
okButton.translatesAutoresizingMaskIntoConstraints = false
okButton.backgroundColor = .systemBlue
okButton.setTitle("OK", for: .normal)
okButton.setTitleColor(.white, for: .normal)
okButton.layer.cornerRadius = 20
addSubview(titleLabel)
addSubview(subtitleLabel)
addSubview(okButton)
NSLayoutConstraint.activate([
titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 20),
titleLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
subtitleLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 10),
subtitleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16),
subtitleLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16),
okButton.topAnchor.constraint(equalTo: subtitleLabel.bottomAnchor, constant: 10),
okButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16),
okButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16),
okButton.heightAnchor.constraint(equalToConstant: 50),
okButton.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor)
])
}
required init?(coder: NSCoder) {
fatalError()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment