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 = &#91;&#91;UIView alloc&#93; init&#93;;
    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);
    &#91;backView setBackgroundColor:&#91;UIColor whiteColor&#93;&#93;;
    
    UIButton *button = &#91;UIButton buttonWithType:UIButtonTypeCustom&#93;;
    &#91;button addTarget:self action:@selector(openPicker:) forControlEvents:UIControlEventTouchUpInside&#93;;
    
    button.frame = CGRectMake(self.textfield_width - self.textfield_height - self.textfield_position_x,
                              self.textfield_position_y,
                              self.textfield_height,
                              self.textfield_height);
    
    &#91;button setBackgroundColor:&#91;UIColor whiteColor&#93;&#93;;
    &#91;button setBackgroundImage:&#91;UIImage imageNamed:@"dropdown.png"&#93; forState:UIControlStateNormal&#93;;
    
    self.timelabel = &#91;&#91;UILabel alloc&#93; init&#93;;
    self.timelabel.text = self.defaultDate == nil ? &#91;&#91;NSDate date&#93; formatWithoutSec&#93; : &#91;self.defaultDate formatWithoutSec&#93;;
    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;
    
    &#91;self.parent addSubview:backView&#93;;
    &#91;self.parent addSubview:self.timelabel&#93;;
    &#91;self.parent addSubview:button&#93;;
}

-(void)createSheet {
    self.opened = YES;
    self.sheet = &#91;&#91;UIView alloc&#93; init&#93;;
    self.sheet.frame = CGRectMake(self.textfield_position_x,
                                  self.textfield_position_y + self.textfield_height,
                                  self.textfield_width - 2 * self.textfield_position_x,
                                  0);
    &#91;self.sheet setBackgroundColor:&#91;UIColor whiteColor&#93;&#93;;
    
    self.sheet.layer.opacity = 0.0;
    
    self.picker = &#91;&#91;UIDatePicker alloc&#93; init&#93;;
    self.picker.frame = CGRectMake(0, 0, self.sheet.frame.size.width, self.sheet.frame.size.height);
    &#91;self.sheet addSubview:self.picker&#93;;
    &#91;self.parent addSubview:self.sheet&#93;;
    &#91;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) {
                              }&#93;;
}

#pragma mark - Helper
-(NSDate *)getDate {
    return &#91;self.timelabel.text parseWithoutSec&#93;;
}

#pragma mark - Event
-(void)openPicker:(id)sender {
    if (!self.opened) {
        &#91;self createSheet&#93;;
    }
    else {
        self.timelabel.text = &#91;&#91;self.picker date&#93; formatWithoutSec&#93;;
        self.opened = NO;
        &#91;self.sheet removeFromSuperview&#93;;
        self.sheet = nil;
    }
}
@end
&#91;/cpp&#93;

<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.
TextDatePicker1

textdatepicker