The MTZExpirationDatePicker is a simple component that subclasses UIPickerView and provides an easy and drop-in way to use it with your project. It also allows the setup of a minimum and maximum date.
Simply drop the files MTZExpirationDatePicker.h and MTZExpirationDatePicker.m to your project and import wherever needed.
You can also declare the following on your Podfile:
pod 'MTZExpirationDatePicker', '~> 1.0.1'Using the picker is relatively straight forward. Most commonly as an inputView as such:
self.expirationDatePicker = [[MTZExpirationDatePicker alloc] init];
self.expirationDatePicker.datePickerDelegate = self; // Needed as UIPickerView doesn't inherited from UIControl.
if (minimumDate) {
self.expirationDatePicker.minimumDate = minimumDate;
}
if (maximumDate) {
self.expirationDatePicker.maximumDate = maximumDate;
}
[self.textField setInputView:self.expirationDatePicker];Then on the delegate just implement the method to know whenever the date has been changed:
#pragma mark - MTZDatePickerDelegate
- (void)datePickerDidChangeDate:(MTZExpirationDatePicker *)datePicker {
self.textField.text = [self.dateFormatter stringFromDate:datePicker.date];
}or just access the property date whenever needed.
self.textField.text = [self.dateFormatter stringFromDate:self.expirationDatePicker.date];You can also set a date manually, and it will respect the minimum and maximum dates, if any:
[self.expirationDatePicker setDate:[NSDate date] animated:YES];If you don't fancy the separator, just disable it:
self.expirationDatePicker.showsSeparator = NO;That's pretty much it. Enjoy! :)
MTZExpirationDatePicker is released under the MIT license. See LICENSE for details.
