@@ -2,7 +2,6 @@ import Foundation
22import UIKit
33import QuartzCore
44
5-
65class DatePickerDialog : UIView {
76
87 typealias DatePickerCallback = ( date: NSDate ) -> Void
@@ -37,10 +36,6 @@ class DatePickerDialog: UIView {
3736 fatalError ( " init(coder:) has not been implemented " )
3837 }
3938
40- deinit {
41- NSNotificationCenter . defaultCenter ( ) . removeObserver ( self )
42- }
43-
4439 func setupView( ) {
4540 self . dialogView = createContainerView ( )
4641
@@ -56,21 +51,15 @@ class DatePickerDialog: UIView {
5651 self . backgroundColor = UIColor ( red: 0 , green: 0 , blue: 0 , alpha: 0 )
5752
5853 self . addSubview ( self . dialogView!)
59-
60- UIApplication . sharedApplication ( ) . windows. first!. addSubview ( self )
6154 }
6255
6356 /* Handle device orientation changes */
6457 func deviceOrientationDidChange( notification: NSNotification ) {
65- /* TODO */
58+ close ( ) // For now just close it
6659 }
6760
6861 /* Create the dialog view, and animate opening the dialog */
69- func show( title: String , datePickerMode: UIDatePickerMode = . DateAndTime, callback: DatePickerCallback ) {
70- show ( title, doneButtonTitle: " Done " , cancelButtonTitle: " Cancel " , datePickerMode: datePickerMode, callback: callback)
71- }
72-
73- func show( title: String , doneButtonTitle: String , cancelButtonTitle: String , defaultDate: NSDate = NSDate ( ) , datePickerMode: UIDatePickerMode = . DateAndTime, callback: DatePickerCallback ) {
62+ func show( title: String , doneButtonTitle: String = " Done " , cancelButtonTitle: String = " Cancel " , defaultDate: NSDate = NSDate ( ) , datePickerMode: UIDatePickerMode = . DateAndTime, callback: DatePickerCallback ) {
7463 self . titleLabel. text = title
7564 self . doneButton. setTitle ( doneButtonTitle, forState: . Normal)
7665 self . cancelButton. setTitle ( cancelButtonTitle, forState: . Normal)
@@ -80,6 +69,12 @@ class DatePickerDialog: UIView {
8069 self . datePicker. datePickerMode = self . datePickerMode ?? . Date
8170 self . datePicker. date = self . defaultDate ?? NSDate ( )
8271
72+ /* */
73+ UIApplication . sharedApplication ( ) . windows. first!. addSubview ( self )
74+ UIApplication . sharedApplication ( ) . windows. first!. endEditing ( true )
75+
76+ NSNotificationCenter . defaultCenter ( ) . addObserver ( self , selector: " deviceOrientationDidChange: " , name: UIDeviceOrientationDidChangeNotification, object: nil )
77+
8378 /* Anim */
8479 UIView . animateWithDuration (
8580 0.2 ,
@@ -96,6 +91,8 @@ class DatePickerDialog: UIView {
9691
9792 /* Dialog close animation then cleaning and removing the view from the parent */
9893 private func close( ) {
94+ NSNotificationCenter . defaultCenter ( ) . removeObserver ( self )
95+
9996 let currentTransform = self . dialogView. layer. transform
10097
10198 let startRotation = ( self . valueForKeyPath ( " layer.transform.rotation.z " ) as? NSNumber ) as? Double ?? 0.0
@@ -194,6 +191,7 @@ class DatePickerDialog: UIView {
194191 self . cancelButton. setTitleColor ( UIColor ( red: 0.2 , green: 0.2 , blue: 0.2 , alpha: 0.5 ) , forState: UIControlState . Highlighted)
195192 self . cancelButton. titleLabel!. font = UIFont . boldSystemFontOfSize ( 14 )
196193 self . cancelButton. layer. cornerRadius = kDatePickerDialogCornerRadius
194+ self . cancelButton. addTarget ( self , action: " buttonTapped: " , forControlEvents: UIControlEvents . TouchUpInside)
197195 container. addSubview ( self . cancelButton)
198196
199197 self . doneButton = UIButton ( type: UIButtonType . Custom) as UIButton
@@ -222,7 +220,7 @@ class DatePickerDialog: UIView {
222220
223221 /* Helper function: count and return the screen's size */
224222 func countScreenSize( ) -> CGSize {
225- let screenWidth = UIScreen . mainScreen ( ) . bounds . size. width
223+ let screenWidth = UIScreen . mainScreen ( ) . applicationFrame . size. width
226224 let screenHeight = UIScreen . mainScreen ( ) . bounds. size. height
227225
228226 return CGSizeMake ( screenWidth, screenHeight)
0 commit comments