-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPMDateViewController.m
127 lines (106 loc) · 4.56 KB
/
PMDateViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// PMDateViewController.m
// UIPopoverMenuController - UIDatePicker选择
//
// Created by WeeverLu on 14-1-24.
// Copyright (c) 2014年 Kinth. All rights reserved.
//
#import "PMDateViewController.h"
#import "PMHeader.h"
@interface PMDateViewController ()
@end
@implementation PMDateViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// 设置控件属性
// ...
self.title = @"请选择时间";
// 设置UIDatePicker模式
if (self.datePickerMode) {
self.datePicker.datePickerMode = self.datePickerMode;
}
self.datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_cn"];
// iPad为导航状态
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// 左边取消按钮
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:(UIBarButtonItemStyleBordered) target:self action:@selector(cancleAction:)];
leftItem.tintColor = [UIColor blackColor];
self.navigationItem.leftBarButtonItem = leftItem;
// 右边确定按钮
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:(UIBarButtonItemStyleDone) target:self action:@selector(doneAction:)];
rightItem.tintColor = [UIColor blackColor];
self.navigationItem.rightBarButtonItem = rightItem;
// 增加self.view高度,修改pickerFrame.origin.y
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height+44);
CGRect pickerFrame = self.datePicker.frame;
pickerFrame.size.height += 44;
self.datePicker.frame = pickerFrame;
}
else {
// 左边取消按钮
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:(UIBarButtonItemStyleBordered) target:self action:@selector(cancleAction:)];
// 右边确定按钮
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:(UIBarButtonItemStyleDone) target:self action:@selector(doneAction:)];
// 中间提示
//UIBarButtonItem *tipsItem = [[UIBarButtonItem alloc] initWithTitle:@"请选择时间" style:(UIBarButtonItemStylePlain) target:self action:nil];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
label.backgroundColor = [UIColor clearColor];
label.text = @"请选择时间";
label.textColor = (kOVER_iOS7)?[UIColor blackColor]:[UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:17];
UIBarButtonItem *tipsItem = [[UIBarButtonItem alloc] initWithCustomView:label];
// 间距
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:self action:nil];
// toolbar
NSArray *toolItems = @[ leftItem, space, tipsItem, space, rightItem ];
UIToolbar *tool = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
tool.tintColor = [UIColor blackColor];
tool.items = toolItems;
self.toolbar = tool;
[self.view addSubview:tool];
// 增加self.view高度,修改pickerFrame.origin.y
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height+44);
CGRect pickerFrame = self.datePicker.frame;
pickerFrame.origin.y = 44;
self.datePicker.frame = pickerFrame;
}
}
- (IBAction)cancleAction:(UIBarButtonItem *)sender
{
if (self.dateSelectedBlock) {
self.dateSelectedBlock(nil, NO);
}
}
- (IBAction)doneAction:(UIBarButtonItem *)sender
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 设置日期显示格式
if (self.dateFormatter) {
formatter.dateFormat = self.dateFormatter;
}
else {
formatter.dateFormat = kDEFAULT_DATEFORMAT;
}
// 当前时间转为字符串
NSString *dateString = [formatter stringFromDate:self.datePicker.date];
if (self.dateSelectedBlock) {
self.dateSelectedBlock(dateString, YES);
}
}
@end