Last active
January 14, 2026 05:16
-
-
Save kopyl/769152472c65d8ac60447a015ce869a0 to your computer and use it in GitHub Desktop.
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
| import UIKit | |
| private func stopScrolling(in view: UIView) { | |
| if let scrollView = view as? UIScrollView { | |
| scrollView.setContentOffset(scrollView.contentOffset, animated: false) | |
| } | |
| for subview in view.subviews { | |
| stopScrolling(in: subview) | |
| } | |
| } | |
| class MainView: UIView { | |
| private let datePicker = UIDatePicker() | |
| private let resetTimeButton = UIButton(type: .system) | |
| private let date = Date() | |
| init() { | |
| super.init(frame: .zero) | |
| datePicker.preferredDatePickerStyle = .wheels | |
| datePicker.date = date | |
| addSubview(datePicker) | |
| datePicker.translatesAutoresizingMaskIntoConstraints = false | |
| datePicker.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true | |
| datePicker.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true | |
| resetTimeButton.setTitle("Reset time", for: .normal) | |
| addSubview(resetTimeButton) | |
| resetTimeButton.translatesAutoresizingMaskIntoConstraints = false | |
| resetTimeButton.centerYAnchor.constraint(equalTo: datePicker.bottomAnchor, constant: 20).isActive = true | |
| resetTimeButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -50).isActive = true | |
| resetTimeButton.addTarget(self, action: #selector(resetTime), for: .touchUpInside) | |
| } | |
| @objc private func resetTime() { | |
| let randomNanoseconds = Int.random(in: 0..<1_000_000_000) | |
| let dateWithRandomNanoseconds = Calendar.current.date(bySetting: .nanosecond, value: randomNanoseconds, of: date) ?? date | |
| datePicker.date = dateWithRandomNanoseconds | |
| } | |
| required init?(coder: NSCoder) { | |
| fatalError("init(coder:) has not been implemented") | |
| } | |
| } | |
| class MainViewController: UIViewController { | |
| override func loadView() { | |
| view = MainView() | |
| } | |
| override func viewDidLoad() { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment