Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added Cancelable to dismiss #103

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.mdown
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 15 additions & 1 deletion Sources/DatePickerDialog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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!
Expand All @@ -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()
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -300,6 +309,11 @@ open class DatePickerDialog: UIView {

close()
}

// triggered when view is tapped
@objc func viewForCancelTapped() {
close()
}

deinit {
NotificationCenter.default.removeObserver(self)
Expand Down