iOS自定义UIDatePicker日期选择器视图

iOS自定义UIDatePicker日期选择器视图 ,首先看一下效果图:

下面贴上相关代码:

ViewController:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

#import "ViewController.h"
#import "HWDatePicker.h"

#define mainW [UIScreen mainScreen].bounds.size.width
#define mainH [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UITextFieldDelegate, HWDatePickerDelegate>

@property (nonatomic, weak) HWDatePicker *datePicker;
@property (nonatomic, strong) UITextField *dateTextField;

@end

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];

 self.view.backgroundColor = [UIColor blackColor];

 //创建控件
 [self creatControl];
}

- (void)creatControl
{
 //textField
 _dateTextField = [[UITextField alloc] initWithFrame:CGRectMake(mainW * 0.05, mainW * 0.72, mainW * 0.9, mainW * 0.12)];
 _dateTextField.background = [UIImage imageNamed:@"textFieldBj"];
 _dateTextField.textAlignment = NSTextAlignmentRight;
 _dateTextField.placeholder = @"请设置日期";
 _dateTextField.delegate = self;
 UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, mainW * 0.4, mainW * 0.12)];
 lab2.textAlignment = NSTextAlignmentLeft;
 lab2.text = @" 日期";
 lab2.textColor = [UIColor grayColor];
 _dateTextField.leftView = lab2;
 _dateTextField.leftViewMode = UITextFieldViewModeAlways;
 UILabel *lab22 = [[UILabel alloc] initWithFrame:CGRectMake(mainW * 0.12 - 15, 0, 15, mainW * 0.12)];
 _dateTextField.rightView = lab22;
 _dateTextField.rightViewMode = UITextFieldViewModeAlways;
 [self.view addSubview:_dateTextField];

 //日期选择器
 HWDatePicker *datePicker = [[HWDatePicker alloc] initWithFrame:CGRectMake(mainW * 0.05, mainH, mainW * 0.9, mainW * 0.5)];
 datePicker.delegate = self;
 [self.view addSubview:datePicker];
 self.datePicker = datePicker;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
 if (_datePicker.frame.origin.y != mainH && _datePicker != nil) {
  [_datePicker dismiss];
  return NO;

 }else if (textField == _dateTextField) {
  [_datePicker show];
  return NO;
 }

 return YES;
}

#pragma mark - HWDatePickerDelegate
- (void)datePickerView:(HWDatePicker *)datePickerView didClickSureBtnWithSelectDate:(NSString *)date
{
 _dateTextField.text = date;
}

@end

HWDatePicker:

#import <UIKit/UIKit.h>

@class HWDatePicker;

@protocol HWDatePickerDelegate <NSObject>

/**
 * HWDatePicker确定按钮点击代理事件
 *
 * @param datePickerView HWDatePicker
 * @param date   选中的日期
 */
- (void)datePickerView:(HWDatePicker *)datePickerView didClickSureBtnWithSelectDate:(NSString *)date;

@end

@interface HWDatePicker : UIView

@property (nonatomic, weak) id<HWDatePickerDelegate> delegate;

- (void)show;
- (void)dismiss;

@end

#import "HWDatePicker.h"

//获得屏幕的宽高
#define mainW [UIScreen mainScreen].bounds.size.width
#define mainH [UIScreen mainScreen].bounds.size.height

@interface HWDatePicker ()

@property (nonatomic, strong) UIDatePicker *datePicker;

@end

@implementation HWDatePicker

- (id)initWithFrame:(CGRect)frame
{
 if (self = [super initWithFrame:frame]) {
  //背景框
  UIImageView *back = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
  back.image = [UIImage imageNamed:@"datePickerBj"];
  [self addSubview:back];

  //日期选择器
  _datePicker = [[UIDatePicker alloc] init];
  _datePicker.frame = CGRectMake(10, 10, self.frame.size.width - 20, 120);
  _datePicker.backgroundColor = [UIColor clearColor];
  [_datePicker setDatePickerMode:UIDatePickerModeDate];
  NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
  _datePicker.locale = locale;
  NSDateFormatter *formatter_minDate = [[NSDateFormatter alloc] init];
  [formatter_minDate setDateFormat:@"yyyy-MM-dd"];
  NSDate *minDate = [formatter_minDate dateFromString:@"2008-01-01"];
  formatter_minDate = nil;
  [_datePicker setMinimumDate:minDate];
  [self addSubview:_datePicker];

  //确定按钮
  UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake((self.frame.size.width - mainW * 0.36) * 0.5, self.frame.size.height * 0.747, mainW * 0.36, mainW * 0.11)];
  [sureBtn setImage:[UIImage imageNamed:@"sureBtn"] forState:UIControlStateNormal];
  [sureBtn addTarget:self action:@selector(sureBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
  [self addSubview:sureBtn];
 }

 return self;
}

- (void)sureBtnOnClick
{
 [self dismiss];

 if (_delegate && [_delegate respondsToSelector:@selector(datePickerView:didClickSureBtnWithSelectDate:)]) {
  [_delegate datePickerView:self didClickSureBtnWithSelectDate:[self getDateString]];
 }
}

- (NSString *)getDateString
{
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setDateFormat:@"yyyy-MM-dd"];
 NSString *date = [dateFormatter stringFromDate:[self.datePicker date]];

 return date;
}

- (void)show
{
 [UIView animateWithDuration:0.3 animations:^{
  self.frame = CGRectMake(mainW * 0.05, mainH - mainW * 0.75, mainW * 0.9, mainW * 0.5);
 }];
}

- (void)dismiss
{
 [UIView animateWithDuration:0.3 animations:^{
  self.frame = CGRectMake(mainW * 0.05, mainH, mainW * 0.9, mainW * 0.5);
 }];
}

@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • iOS 获取公历、农历日期的年月日的实例代码

    介绍三种方法获取 Date (NSDate) 的年月日. 用 date 表示当前日期.测试日期为公历 2017 年 2 月 5 日,农历丁酉年,鸡年,正月初九. let date: Date = Date() NSDate *date = [NSDate date]; 获取公历年月日 用 Calendar (NSCalendar) 获取公历年月日 let calendar: Calendar = Calendar(identifier: .gregorian) print("Year:"

  • iOS实现自定义日期选择器示例

    iOS自定义日期选择器,下面只是说明一下怎么用,具体实现请在最后下载代码看看: 效果如下: .h文件解析 选择日期选择器样式 typedef enum{ DateStyleShowYearMonthDayHourMinute = 0, DateStyleShowMonthDayHourMinute, DateStyleShowYearMonthDay, DateStyleShowMonthDay, DateStyleShowHourMinute }XHDateStyle; //日期选择器样式 @

  • iOS自定义UIDatepicker日期选择器视图分享

    由于项目需要,需要定制一个日期选择器,找了半天没找到合适的就自己写了个demo 自定义UIDatePicker日期选择器视图 效果如下: 下面贴上相关代码: ViewController: <pre name="code" class="objc">- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically

  • iOS自定义日期和数据源选择控件

    需求 App开发中经常会有日期选择(如生日.睡眠定时等)或者省市区选择等此类功能,通常UI中不会单独使用UI中的控件,而是在UIPickerView的基础上增加一个取消和确定按钮 特点 1.支持常见的选择型的数据格式 该控件集成了 yyyy-MM-dd.yyyy-MM.hh mm.省市级联.省市区级联.自定义数据源(2列).自定义数据源(3列)等多种格式 2.即支持UITextField又支持事件触发机制 3.即支持XIB也支持纯代码 效果图 GitHub:XXPickerView 集成 首先将

  • iOS计算上次日期距离现在多久的代码

    本文实例为大家分享了iOS上次日期距离现在多久的计算代码,供大家参考,具体内容如下 /** * 计算上次日期距离现在多久 * * @param lastTime 上次日期(需要和格式对应) * @param format1 上次日期格式 * @param currentTime 最近日期(需要和格式对应) * @param format2 最近日期格式 * * @return xx分钟前.xx小时前.xx天前 */ + (NSString *)timeIntervalFromLastTime:(

  • iOS自定义日期demo分享

    有个项目需求是做个在日期上选择的,就是这种: 网上看了几个日期的demo都太厚重了,移植起来太麻烦,然后打算自己写. 就先写个简化的demo看看,主要有几个关键点: 首先要根据当前日期获取这个月有几天 然后判断这个月份第一天是周几 再根据上面两个数据在合理的位置显示数据 还要记录下当前的日期方便切换月份 如果调接口的话其实根据后台给数据比对下对应的日期展示数据即可 其中有一个容易迷糊的是获取的星期天是第一天,下标是1 所以我们的数组是这样的 _weekdays = [NSArray arrayW

  • Android开发中实现IOS风格底部选择器(支持时间 日期 自定义)

    本文Github代码链接 https://github.com/AndroidMsky/AndoirdIOSPicker 先上图吧: 这是笔者最近一个项目一直再用的一个选择器库,自己也在其中做了修改,并决定持续维护下去. 先看使用方法: 日期选择: private void showDateDialog(List<Integer> date) { DatePickerDialog.Builder builder = new DatePickerDialog.Builder(this); bui

  • iOS自定义日期、时间、城市选择器实例代码

    选择器,我想大家都不陌生,当需要用户去选择某些范围值内的一个固定值时,我们会采用选择器的方式.选择器可以直观的提示用户选择的值范围.统一信息的填写格式,同时也方便用户快速的进行选择,比如对于性别,正常情况下就只有男女两种情况,那这时候用一个选择器给用户进行选择的话,可以避免错误数据的输入,也更方便用户去填写.再比如需要获取用户的生日信息时,采用选择器的方式可以统一生日的格式,如果让用户自行输入的话,可能会出现各种各样的生日信息格式,不利于数据的存储,但是采用选择器的方式的话,用户可找到对应的日期

  • iOS获取某个日期后n个月的日期

    一.给一个时间,给一个数,正数是以后n个月,负数是前n个月: -(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(NSInteger)month { NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setMonth:month]; NSCalendar *calender = [[NSCalendar alloc] initWithC

  • iOS如何获取当前日期前后N天的时间示例代码

    前言 记得之前看过一部有关机器人动画片,具体名字叫什么忘记了.但是其中有句台词我记得还是很清楚的 明年的今日就是你的忌日. 联系到iOS,如果在项目中遇到了计算日期的,并且是要获取当前时间(指定日期)n天后的日期,这可该怎么实现呢? 比如我们要获取当前日期7天后的日期,要怎么实现呢 获取当前日期 NSDate *currentDate = [NSDate date]; 前一天或后一天时间 NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*

随机推荐