diff --git a/README.mdown b/README.mdown index 599477a..8173745 100644 --- a/README.mdown +++ b/README.mdown @@ -61,6 +61,7 @@ github "squimer/DatePickerDialog-iOS-Swift" ## Dialog parameters - showCancelButton: Bool - default true +- cancelable: Bool - default true - locale: Locale? - default nil Example without 'Cancel' button: diff --git a/Sources/DatePickerDialog.swift b/Sources/DatePickerDialog.swift index 78d2611..fdd0f5c 100644 --- a/Sources/DatePickerDialog.swift +++ b/Sources/DatePickerDialog.swift @@ -4,6 +4,8 @@ import UIKit private extension Selector { static let buttonTapped = #selector(DatePickerDialog.buttonTapped) static let deviceOrientationDidChange = #selector(DatePickerDialog.deviceOrientationDidChange) + static let viewForCancelTapped = #selector(DatePickerDialog.viewForCancelTapped) + } open class DatePickerDialog: UIView { @@ -27,6 +29,7 @@ open class DatePickerDialog: UIView { private var datePickerMode: UIDatePicker.Mode? private var callback: DatePickerCallback? var showCancelButton: Bool = false + var cancelable: Bool = false var locale: Locale? private var textColor: UIColor! @@ -38,13 +41,14 @@ open class DatePickerDialog: UIView { buttonColor: UIColor = UIColor.blue, font: UIFont = .boldSystemFont(ofSize: 15), locale: Locale? = nil, - showCancelButton: Bool = true) { + showCancelButton: Bool = true, cancelable: Bool = true) { let size = UIScreen.main.bounds.size super.init(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height)) self.textColor = textColor self.buttonColor = buttonColor self.font = font self.showCancelButton = showCancelButton + self.cancelable = cancelable self.locale = locale setupView() } @@ -227,6 +231,11 @@ open class DatePickerDialog: UIView { // Add the buttons addButtonsToView(container: container) + + if cancelable { + let tap = UITapGestureRecognizer(target: self, action: .viewForCancelTapped) + self.addGestureRecognizer(tap) + } return container } @@ -300,6 +309,11 @@ open class DatePickerDialog: UIView { close() } + + // triggered when view is tapped + @objc func viewForCancelTapped() { + close() + } deinit { NotificationCenter.default.removeObserver(self)