TextDatePicker(fold UIDatePicker in UIView)
UIDatePicker is big for screen?
If we add UIDatePicker in UI, it occupied large area in view.
So, I create TextDatePicker for it.
Architecture
Sample
First these are helpers. These are date and string operations.
NSString+DateFormat.h
#import <Foundation/Foundation.h> @interface NSString (DateFormat) -(NSDate *)parseWithoutSec; @end
NSString+DateFormat.m
#import "NSString+DateFormat.h"
@implementation NSString (DateFormat)
-(NSDate *)parseWithoutSec {
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm"];
return [formatter dateFromString:self];
}
@end
NSDate+DateFormat.h
#import <Foundation/Foundation.h> @interface NSDate (DateFormat) -(NSString *)formatWithoutSec; @end
NSDate+DateFormat.m
#import "NSDate+DateFormat.h"
@implementation NSDate (DateFormat)
-(NSString *)formatWithoutSec {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy/MM/dd HH:mm"];
return [formatter stringFromDate:self];
}
@end
Following: This is main part
TextDatePicker
TextDatePicker.h
#import <UIKit/UIKit.h> @interface TextDatePicker : UIView @property (nonatomic, weak) UIView *parent; @property (nonatomic) double textfield_width; @property (nonatomic) double textfield_height; @property (nonatomic) double textfield_position_x; @property (nonatomic) double textfield_position_y; @property (nonatomic) NSDate *defaultDate; @property (nonatomic) double duration; -(NSDate *)getDate; -(void)show; @end
TextDatePicker.m
#import "TextDatePicker.h"
#import "NSDate+DateFormat.h"
#import "NSString+DateFormat.h"
@interface TextDatePicker()
@property (nonatomic) UILabel *timelabel;
@property (nonatomic) UIView *sheet;
@property (nonatomic) BOOL opened;
@property (nonatomic) UIDatePicker *picker;
@end
@implementation TextDatePicker
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
#pragma mark - Create UI
-(void)show {
if ( self.parent == nil
&& self.textfield_width > self.parent.frame.size.width
&& self.textfield_height > self.parent.frame.size.height
&& self.textfield_height < 0
&& self.textfield_position_x < 0) {
// Do nothing
return;
}
self.opened = NO;
// Default Value
if ( self.textfield_height == 0 ) {
self.textfield_height = 50;
}
if ( self.textfield_width == 0 ) {
self.textfield_width = self.parent.frame.size.width;
}
UIView *backView = [[UIView alloc] init];
backView.frame = CGRectMake(self.textfield_position_x,
self.textfield_position_y,
self.textfield_width - self.textfield_height - 2 * self.textfield_position_x,
self.textfield_height);
[backView setBackgroundColor:[UIColor whiteColor]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(openPicker:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(self.textfield_width - self.textfield_height - self.textfield_position_x,
self.textfield_position_y,
self.textfield_height,
self.textfield_height);
[button setBackgroundColor:[UIColor whiteColor]];
[button setBackgroundImage:[UIImage imageNamed:@"dropdown.png"] forState:UIControlStateNormal];
self.timelabel = [[UILabel alloc] init];
self.timelabel.text = self.defaultDate == nil ? [[NSDate date] formatWithoutSec] : [self.defaultDate formatWithoutSec];
self.timelabel.frame = CGRectMake(self.textfield_position_x,
self.textfield_position_y,
self.textfield_width - self.textfield_height,
self.textfield_height);
self.timelabel.textAlignment = NSTextAlignmentCenter;
[self.parent addSubview:backView];
[self.parent addSubview:self.timelabel];
[self.parent addSubview:button];
}
-(void)createSheet {
self.opened = YES;
self.sheet = [[UIView alloc] init];
self.sheet.frame = CGRectMake(self.textfield_position_x,
self.textfield_position_y + self.textfield_height,
self.textfield_width - 2 * self.textfield_position_x,
0);
[self.sheet setBackgroundColor:[UIColor whiteColor]];
self.sheet.layer.opacity = 0.0;
self.picker = [[UIDatePicker alloc] init];
self.picker.frame = CGRectMake(0, 0, self.sheet.frame.size.width, self.sheet.frame.size.height);
[self.sheet addSubview:self.picker];
[self.parent addSubview:self.sheet];
[UIView animateKeyframesWithDuration:self.duration
delay:0.0
options:UIViewKeyframeAnimationOptionCalculationModeLinear
animations:^{
self.sheet.frame = CGRectMake(self.textfield_position_x,
self.textfield_position_y + self.textfield_height,
self.textfield_width - 2 * self.textfield_position_x,
160);
self.sheet.layer.opacity = 1.0;
}
completion:^(BOOL finished) {
}];
}
#pragma mark - Helper
-(NSDate *)getDate {
return [self.timelabel.text parseWithoutSec];
}
#pragma mark - Event
-(void)openPicker:(id)sender {
if (!self.opened) {
[self createSheet];
}
else {
self.timelabel.text = [[self.picker date] formatWithoutSec];
self.opened = NO;
[self.sheet removeFromSuperview];
self.sheet = nil;
}
}
@end
[/cpp]
<h3>How to use?</h3>
This is ViewController example
[cpp]
#import "ViewController.h"
#import "TextDatePicker.h"
@interface ViewController ()
@property (nonatomic) TextDatePicker *tpicker;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.tpicker = [[TextDatePicker alloc] init];
self.tpicker.parent = self.view;
self.tpicker.textfield_height = 45;
self.tpicker.textfield_position_x = 5;
self.tpicker.textfield_position_y = 5;
self.tpicker.duration = 0.5;
[self.view addSubview:self.tpicker];
[self.tpicker show];
[self.view setBackgroundColor:[UIColor blackColor]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Result
Tap arrow button, open DatePicker view.
And tap again, fold DatePicker view and update date you set.


