Skip to content

Instantly share code, notes, and snippets.

@sebjvidal
Created April 30, 2024 15:50
Show Gist options
  • Save sebjvidal/85c9fc270a554b68f37e781f8a07c167 to your computer and use it in GitHub Desktop.
Save sebjvidal/85c9fc270a554b68f37e781f8a07c167 to your computer and use it in GitHub Desktop.
Apple Journal Calendar UI Demo
//
// ViewController.swift
// Journal-Calendar-Demo
//
// Created by Seb Vidal on 30/04/2024.
//
import UIKit
class ViewController: UIViewController {
// MARK: - init(nibName:bundle:)
override init(nibName: String?, bundle: Bundle?) {
super.init(nibName: nibName, bundle: bundle)
let button = UIButton(type: .system)
button.setTitle("Show Calendar", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(showAlertController), for: .touchUpInside)
view.addSubview(button)
NSLayoutConstraint.activate([
button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
}
// MARK: - init(coder:)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Private Methods
@objc private func showAlertController(_ sender: UIButton) {
let selector = NSSelectorFromString("_setHeaderContentViewController:")
let headerContentViewController = CalendarViewController()
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alertController.perform(selector, with: headerContentViewController)
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in})
present(alertController, animated: true)
}
}
class CalendarViewController: UIViewController {
// MARK: - Private Properties
private var calendarView: UICalendarView!
// MARK: - init(nibName:bundle:)
override init(nibName: String?, bundle: Bundle?) {
super.init(nibName: nibName, bundle: bundle)
calendarView = UICalendarView()
calendarView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(calendarView)
NSLayoutConstraint.activate([
calendarView.topAnchor.constraint(equalTo: view.topAnchor),
calendarView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
calendarView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
calendarView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
// MARK: - init(coder:)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - viewDidLayoutSubviews()
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let height = calendarView.intrinsicContentSize.height
preferredContentSize = CGSize(width: -1, height: height)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment