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 iOS 14 DatePickerStyle Support #488

Merged
merged 1 commit into from
Aug 29, 2020
Merged
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
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 - (Inline):",
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 14.0, *) {
datePicker?.datePickerStyle = .inline
}

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 - (Automatic):",
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 13.4, *) {
datePicker?.datePickerStyle = .automatic
}

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;
skywinder marked this conversation as resolved.
Show resolved Hide resolved

+ (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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to pass the datePickerStyle. Default could be .automatic.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you prefer converting this class to Swift or do the datePickerStyle changes in the current Objective C class?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Objective C should be enough in this case.

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