Skip to content

Commit

Permalink
- Fixed crash in iOS 14.0
Browse files Browse the repository at this point in the history
- Added iOS 14 DatePickerStyle Support
  • Loading branch information
Noor ul Ain Ali committed Aug 17, 2020
1 parent be29dab commit c89e57c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ class SWTableViewController: UITableViewController, UITextFieldDelegate {

@IBAction func timePickerClicked(_ sender: UIButton) {
// example of picker initialized with target/action parameters
let datePicker = ActionSheetDatePicker(title: "Time:",
let datePicker = ActionSheetDatePicker(title: "Time - (Automatic):",
datePickerMode: UIDatePicker.Mode.time,
selectedDate: Date(),
target: self,
action: #selector(datePicked(_:)),
origin: sender.superview!.superview)

datePicker?.minuteInterval = 20
if #available(iOS 13.4, *) {
datePicker?.datePickerStyle = .automatic
}

datePicker?.show()
}
Expand All @@ -68,7 +70,7 @@ class SWTableViewController: UITableViewController, UITextFieldDelegate {

@IBAction func datePickerClicked(_ sender: UIButton) {
// example of date picker with min and max values set (as a week in past and week in future from today)
let datePicker = ActionSheetDatePicker(title: "Date within 2 weeks:",
let datePicker = ActionSheetDatePicker(title: "Date within 2 weeks - (Compact):",
datePickerMode: UIDatePicker.Mode.date,
selectedDate: Date(),
doneBlock: { picker, date, origin in
Expand All @@ -84,14 +86,17 @@ class SWTableViewController: UITableViewController, UITextFieldDelegate {
let secondsInWeek: TimeInterval = 7 * 24 * 60 * 60;
datePicker?.minimumDate = Date(timeInterval: -secondsInWeek, since: Date())
datePicker?.maximumDate = Date(timeInterval: secondsInWeek, since: Date())
if #available(iOS 13.4, *) {
datePicker?.datePickerStyle = .compact
}

datePicker?.show()
}


@IBAction func dateAndTimePickerClicked(_ sender: UIButton) {
// example of datetime picker with step interval set to 20 min
let datePicker = ActionSheetDatePicker(title: "DateTime with 20min intervals:",
let datePicker = ActionSheetDatePicker(title: "DateTime with 20min intervals - (Inline):",
datePickerMode: UIDatePicker.Mode.dateAndTime,
selectedDate: Date(),
doneBlock: { picker, date, origin in
Expand All @@ -105,6 +110,9 @@ class SWTableViewController: UITableViewController, UITextFieldDelegate {
},
origin: sender.superview!.superview)
datePicker?.minuteInterval = 20
if #available(iOS 14.0, *) {
datePicker?.datePickerStyle = .inline
}

datePicker?.show()
}
Expand Down
52 changes: 34 additions & 18 deletions Pickers/ActionSheetDatePicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,50 @@ typedef void(^ActionDateCancelBlock)(ActionSheetDatePicker *picker);

@property (nonatomic, assign) NSTimeInterval countDownDuration; // for UIDatePickerModeCountDownTimer, ignored otherwise. default is 0.0. limit is 23:59 (86,399 seconds). value being set is div 60 (drops remaining seconds).

@property (nonatomic, assign) UIDatePickerStyle datePickerStyle API_AVAILABLE(ios(13.4)); // for UIDatePickerStyle, default is Automatic

@property (nonatomic, copy) ActionDateDoneBlock onActionSheetDone;
@property (nonatomic, copy) ActionDateCancelBlock onActionSheetCancel;

+ (instancetype)showPickerWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action origin:(id)origin;

+ (instancetype)showPickerWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action origin:(id)origin cancelAction:(SEL)cancelAction;
+ (instancetype)showPickerWithTitle:(NSString *)title
datePickerMode:(UIDatePickerMode)datePickerMode
selectedDate:(NSDate *)selectedDate
target:(id)target
action:(SEL)action
origin:(id)origin;

+ (instancetype)showPickerWithTitle:(NSString *)title
datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate
minimumDate:(NSDate *)minimumDate maximumDate:(NSDate *)maximumDate
target:(id)target action:(SEL)action origin:(id)origin;
datePickerMode:(UIDatePickerMode)datePickerMode
selectedDate:(NSDate *)selectedDate
target:(id)target
action:(SEL)action
origin:(id)origin
cancelAction:(SEL)cancelAction;

+ (instancetype)showPickerWithTitle:(NSString *)title
datePickerMode:(UIDatePickerMode)datePickerMode
selectedDate:(NSDate *)selectedDate
doneBlock:(ActionDateDoneBlock)doneBlock
cancelBlock:(ActionDateCancelBlock)cancelBlock
origin:(UIView*)view;
datePickerMode:(UIDatePickerMode)datePickerMode
selectedDate:(NSDate *)selectedDate
minimumDate:(NSDate *)minimumDate
maximumDate:(NSDate *)maximumDate
target:(id)target
action:(SEL)action
origin:(id)origin;

+ (instancetype)showPickerWithTitle:(NSString *)title
datePickerMode:(UIDatePickerMode)datePickerMode
selectedDate:(NSDate *)selectedDate
minimumDate:(NSDate *)minimumDate
maximumDate:(NSDate *)maximumDate
doneBlock:(ActionDateDoneBlock)doneBlock
cancelBlock:(ActionDateCancelBlock)cancelBlock
origin:(UIView*)view;
datePickerMode:(UIDatePickerMode)datePickerMode
selectedDate:(NSDate *)selectedDate
doneBlock:(ActionDateDoneBlock)doneBlock
cancelBlock:(ActionDateCancelBlock)cancelBlock
origin:(UIView*)view;

+ (instancetype)showPickerWithTitle:(NSString *)title
datePickerMode:(UIDatePickerMode)datePickerMode
selectedDate:(NSDate *)selectedDate
minimumDate:(NSDate *)minimumDate
maximumDate:(NSDate *)maximumDate
doneBlock:(ActionDateDoneBlock)doneBlock
cancelBlock:(ActionDateCancelBlock)cancelBlock
origin:(UIView*)view;

- (id)initWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action origin:(id)origin;

Expand Down
13 changes: 9 additions & 4 deletions Pickers/ActionSheetDatePicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
#import <objc/message.h>

@interface ActionSheetDatePicker()

@property (nonatomic, assign) UIDatePickerMode datePickerMode;
@property (nonatomic, strong) NSDate *selectedDate;

@end

@implementation ActionSheetDatePicker
Expand Down Expand Up @@ -164,10 +166,13 @@ - (UIView *)configuredPickerView {
datePicker.calendar = self.calendar;
datePicker.timeZone = self.timeZone;
datePicker.locale = self.locale;

UIColor *textColor = [self.pickerTextAttributes valueForKey:NSForegroundColorAttributeName];
if (textColor) {
[datePicker setValue:textColor forKey:@"textColor"]; // use ObjC runtime to set value for property that is not exposed publicly
if (@available(iOS 13.4, *)) {
datePicker.preferredDatePickerStyle = self.datePickerStyle;
} else {
UIColor *textColor = [self.pickerTextAttributes valueForKey:NSForegroundColorAttributeName];
if (textColor) {
[datePicker setValue:textColor forKey:@"textColor"]; // use ObjC runtime to set value for property that is not exposed publicly
}
}

// if datepicker is set with a date in countDownMode then
Expand Down

0 comments on commit c89e57c

Please sign in to comment.