《省钱达人》与《猎豆优选》UI相同版。域名tbk

ITDatePickerController.m 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. //
  2. // ITDatePickerController.m
  3. // ITDatePickerControllerSampleCode
  4. //
  5. // Created by Wit on 2017/8/10.
  6. // Copyright © 2017年 Wit. All rights reserved.
  7. //
  8. #import "ITDatePickerController.h"
  9. #define kITScreenWidth ([[UIScreen mainScreen] bounds].size.width)
  10. #define kITScreenHeight ([[UIScreen mainScreen] bounds].size.height)
  11. static NSInteger kITDatePickerViewHeight = 216+44;
  12. #define kITLocalDate(date) \
  13. \
  14. NSTimeZone *zone = [NSTimeZone systemTimeZone];\
  15. NSInteger interval = [zone secondsFromGMTForDate:date];\
  16. date = [date dateByAddingTimeInterval:interval]
  17. #pragma mark - ***************** ITDatePickerController *************
  18. #pragma mark -
  19. @interface ITDatePickerController ()
  20. <UIViewControllerTransitioningDelegate,
  21. ITDatePickerControllerDelegate>
  22. @property (strong, nonatomic, readwrite) ITDatePickerView *datePickerView;
  23. @property (strong, nonatomic, readwrite) UIView *backgroundView;
  24. @end
  25. @implementation ITDatePickerController
  26. #pragma mark - init
  27. #pragma mark -
  28. - (instancetype)init
  29. {
  30. if (self = [super init]) {
  31. [self configDatePickerController];
  32. }
  33. return self;
  34. }
  35. - (void)awakeFromNib {
  36. [super awakeFromNib];
  37. [self configDatePickerController];
  38. }
  39. - (id)initWithCoder:(NSCoder *)aDecoder
  40. {
  41. if (self = [super initWithCoder:aDecoder]) {
  42. [self configDatePickerController];
  43. }
  44. return self;
  45. }
  46. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  47. {
  48. if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
  49. [self configDatePickerController];
  50. }
  51. return self;
  52. }
  53. #pragma mark - life cycle
  54. - (void)viewDidLoad {
  55. [super viewDidLoad];
  56. self.view.backgroundColor = [UIColor clearColor];
  57. [self.view insertSubview:self.backgroundView atIndex:0];
  58. [self.view addSubview:self.datePickerView];
  59. [self addSingleTapGestures];
  60. }
  61. #pragma mark - Public
  62. #pragma mark -
  63. - (void)refreshData {
  64. [self.datePickerView refreshData];
  65. }
  66. #pragma mark - IBActions
  67. #pragma mark -
  68. - (void)singleTap:(UITapGestureRecognizer *)sender {
  69. [self dismissViewControllerAnimated:YES completion:nil];
  70. }
  71. #pragma mark - Private
  72. #pragma mark -
  73. - (void)configDatePickerController {
  74. self.providesPresentationContextTransitionStyle = YES;
  75. self.definesPresentationContext = YES;
  76. self.modalPresentationStyle = UIModalPresentationCustom;
  77. self.transitioningDelegate = self;
  78. }
  79. - (void)addSingleTapGestures {
  80. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
  81. [self.backgroundView addGestureRecognizer:singleTap];
  82. }
  83. #pragma mark - Delegate Collection
  84. #pragma mark - ITDatePickerControllerDelegate
  85. - (void)datePickerController:(ITDatePickerController *)datePickerController
  86. didSelectedDate:(NSDate *)date
  87. dateString:(NSString *)dateString {
  88. if ([self.delegate respondsToSelector:@selector(datePickerController:didSelectedDate:dateString:)]) {
  89. [self.delegate datePickerController:self didSelectedDate:date dateString:dateString];
  90. }
  91. }
  92. #pragma mark - UIViewControllerTransitioningDelegate
  93. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
  94. {
  95. return [ITAnimation animationWithPresenting:YES];
  96. }
  97. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
  98. {
  99. return [ITAnimation animationWithPresenting:NO];
  100. }
  101. #pragma mark - Custom Accessors
  102. #pragma mark -
  103. - (void)setShowToday:(BOOL)showToday {
  104. [self.datePickerView setShowToday:showToday];
  105. }
  106. - (void)setMaximumDate:(NSDate *)maximumDate {
  107. [self.datePickerView setMaximumDate:maximumDate];
  108. }
  109. - (void)setMinimumDate:(NSDate *)minimumDate {
  110. [self.datePickerView setMinimumDate:minimumDate];
  111. }
  112. - (void)setDefaultYear:(NSInteger)defaultYear {
  113. [self.datePickerView setDefaultYear:defaultYear];
  114. }
  115. - (void)setDefaultMonth:(NSInteger)defaultMonth {
  116. [self.datePickerView setDefaultMonth:defaultMonth];
  117. }
  118. - (void)setDefaultDate:(NSDate *)defaultDate {
  119. [self.datePickerView setDefaultDate:defaultDate];
  120. }
  121. - (void)setLeftItems:(NSArray *)leftItems {
  122. [self.datePickerView setLeftItems:leftItems];
  123. }
  124. - (void)setRightItems:(NSArray *)rightItems {
  125. [self.datePickerView setRightItems:rightItems];
  126. }
  127. - (ITDatePickerView *)datePickerView {
  128. if (!_datePickerView) {
  129. _datePickerView = [[ITDatePickerView alloc] initWithFrame:CGRectMake(0, kITScreenHeight-kITDatePickerViewHeight, kITScreenWidth, kITDatePickerViewHeight)];
  130. _datePickerView.delegate = self;
  131. }
  132. return _datePickerView;
  133. }
  134. - (UIView *)backgroundView {
  135. if (!_backgroundView) {
  136. _backgroundView = [[UIView alloc] initWithFrame:self.view.bounds];
  137. _backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
  138. }
  139. return _backgroundView;
  140. }
  141. @end
  142. #pragma mark - ***************** ITAnimation *************
  143. #pragma mark -
  144. @implementation ITAnimation
  145. #pragma mark - Init
  146. #pragma mark -
  147. - (instancetype)initWithPresenting:(BOOL)isPresenting {
  148. self = [super init];
  149. if (self) {
  150. _presenting = isPresenting;
  151. }
  152. return self;
  153. }
  154. + (instancetype)animationWithPresenting:(BOOL)isPresenting {
  155. return [[ITAnimation alloc] initWithPresenting:isPresenting];
  156. }
  157. #pragma mark - Private
  158. #pragma mark -
  159. - (void)presentAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
  160. {
  161. ITDatePickerController *toViewController = (ITDatePickerController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  162. toViewController.backgroundView.alpha = 0.0;
  163. toViewController.datePickerView.transform = CGAffineTransformMakeTranslation(0, CGRectGetHeight(toViewController.datePickerView.frame));
  164. UIView *containerView = [transitionContext containerView];
  165. [containerView addSubview:toViewController.view];
  166. [UIView animateWithDuration:0.25 animations:^{
  167. toViewController.backgroundView.alpha = 1.0;
  168. } completion:nil];
  169. [UIView animateWithDuration:0.25
  170. delay:0
  171. options:UIViewAnimationOptionCurveEaseOut
  172. animations:^{
  173. toViewController.datePickerView.transform = CGAffineTransformIdentity;
  174. } completion:^(BOOL finished) {
  175. [transitionContext completeTransition:YES];
  176. }];
  177. }
  178. - (void)dismissAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
  179. {
  180. ITDatePickerController *fromViewController = (ITDatePickerController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  181. fromViewController.datePickerView.transform = CGAffineTransformIdentity;
  182. [UIView animateWithDuration:0.25 animations:^{
  183. fromViewController.backgroundView.alpha = 0.0;
  184. } completion:nil];
  185. [UIView animateWithDuration:0.25
  186. delay:0
  187. options:UIViewAnimationOptionCurveEaseIn
  188. animations:^{
  189. fromViewController.datePickerView.transform = CGAffineTransformMakeTranslation(0, CGRectGetHeight(fromViewController.datePickerView.frame));
  190. } completion:^(BOOL finished) {
  191. [transitionContext completeTransition:YES];
  192. }];
  193. }
  194. #pragma mark - UIViewControllerAnimatedTransitioning
  195. #pragma mark -
  196. - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
  197. {
  198. if (self.isPresenting) {
  199. [self presentAnimateTransition:transitionContext];
  200. }else {
  201. [self dismissAnimateTransition:transitionContext];
  202. }
  203. }
  204. - (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
  205. {
  206. if (self.isPresenting) {
  207. return 0.45;
  208. }
  209. return 0.25;
  210. }
  211. @end
  212. #pragma mark - ***************** IDDatePickerView *************
  213. #pragma mark -
  214. static NSInteger maxCount = 1000;
  215. @interface ITDatePickerView ()
  216. <UIPickerViewDelegate,
  217. UIPickerViewDataSource>
  218. @property (nonatomic) NSInteger yearCount;
  219. @property (nonatomic) NSInteger monthCount;
  220. @property (nonatomic) NSInteger thisYear;
  221. @property (nonatomic) NSInteger thisMonth;
  222. @property (nonatomic) NSInteger thisDay;
  223. @property (nonatomic) NSInteger maximumYear;
  224. @property (nonatomic) NSInteger minimumYear;
  225. @property (nonatomic) NSInteger maximumMonth;
  226. @property (nonatomic) NSInteger minimumMonth;
  227. @property (nonatomic) NSInteger selectedYear;
  228. @property (nonatomic) NSInteger selectedMonth;
  229. @property (strong, nonatomic) NSDateFormatter *dateFormatter;
  230. @property (nonatomic, getter=isHideMonth) BOOL hideMonth;
  231. @property (strong, nonatomic) UIView *line;
  232. @property (strong, nonatomic) UIBarButtonItem *cancelButton;
  233. @property (strong, nonatomic) UIBarButtonItem *confirmButton;
  234. @property (strong, nonatomic, readwrite) UIBarButtonItem *flexibleSpaceItem;
  235. @property (strong, nonatomic, readwrite) UIView *toolBar;
  236. @property (strong, nonatomic) UIPickerView *pickerView;
  237. /**
  238. Cancel the operation
  239. */
  240. - (void)cancelButtonOnClicked:(UIButton *)sender;
  241. /**
  242. Confirm the operation
  243. */
  244. - (void)confirmButtonOnClicked:(UIButton *)sender;
  245. @end
  246. @implementation ITDatePickerView
  247. @synthesize yearCount = _yearCount;
  248. #pragma mark - Init
  249. #pragma mark -
  250. - (instancetype)init
  251. {
  252. self = [super init];
  253. if (self) {
  254. [self initDatePickerView];
  255. }
  256. return self;
  257. }
  258. - (instancetype)initWithFrame:(CGRect)frame
  259. {
  260. self = [super initWithFrame:frame];
  261. if (self) {
  262. [self initDatePickerView];
  263. }
  264. return self;
  265. }
  266. - (void)didMoveToSuperview {
  267. [super didMoveToSuperview];
  268. [self refreshData];
  269. }
  270. #pragma mark - Public
  271. #pragma mark -
  272. - (void)refreshData {
  273. if (self.showToday && self.selectedYear == self.thisYear && self.selectedYear == self.thisYear) {
  274. self.hideMonth = YES;
  275. } else {
  276. self.hideMonth = NO;
  277. }
  278. [self.pickerView reloadAllComponents];
  279. [self setDefaultYear:_defaultYear];
  280. [self setDefaultMonth:_defaultMonth];
  281. }
  282. #pragma mark - IBActions
  283. #pragma mark -
  284. - (void)cancelButtonOnClicked:(UIButton *)sender {
  285. [[self topVC] dismissViewControllerAnimated:YES completion:nil];
  286. }
  287. - (void)confirmButtonOnClicked:(UIButton *)sender {
  288. if ([self.delegate respondsToSelector:@selector(datePickerController:didSelectedDate:dateString:)]) {
  289. NSString *dateString = [NSString stringWithFormat:@"%ld-%02ld", self.selectedYear, self.selectedMonth];
  290. NSDate *selectedDate = [self.dateFormatter dateFromString:dateString];
  291. if (self.selectedYear == self.thisYear &&
  292. self.selectedMonth == self.thisMonth &&
  293. self.showToday) {
  294. dateString = @"至今";
  295. }
  296. kITLocalDate(selectedDate);
  297. [self.delegate datePickerController:(id)self didSelectedDate:selectedDate dateString:dateString];
  298. }
  299. [self cancelButtonOnClicked:nil];
  300. }
  301. #pragma mark - Private
  302. #pragma mark -
  303. - (void)initDatePickerView {
  304. [self initThisDate];
  305. self.maximumDate = [self.dateFormatter dateFromString:@"2018-12"];
  306. self.minimumDate = [self.dateFormatter dateFromString:@"2018-01"];
  307. _showToday = YES;
  308. _monthCount = 12;
  309. self.defaultDate = [NSDate date];
  310. [self addSubview:self.toolBar];
  311. [self addSubview:self.pickerView];
  312. }
  313. - (void)initThisDate {
  314. NSDate *currentDate = [NSDate date];
  315. NSCalendar* calendar = [NSCalendar currentCalendar];
  316. NSDateComponents* components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
  317. _thisYear = [components year];
  318. _thisMonth = [components month];
  319. _thisDay = [components day];
  320. }
  321. - (UIViewController *)topVC {
  322. UIViewController *resultVC;
  323. resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
  324. while (resultVC.presentedViewController) {
  325. resultVC = [self _topViewController:resultVC.presentedViewController];
  326. }
  327. return resultVC;
  328. }
  329. - (UIViewController *)_topViewController:(UIViewController *)vc {
  330. if ([vc isKindOfClass:[UINavigationController class]]) {
  331. return [self _topViewController:[(UINavigationController *)vc topViewController]];
  332. } else if ([vc isKindOfClass:[UITabBarController class]]) {
  333. return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
  334. } else {
  335. return vc;
  336. }
  337. return nil;
  338. }
  339. - (void)resetToolbar{
  340. // NSMutableArray *items = [NSMutableArray array];
  341. // if (self.leftItems.count>0) {
  342. // [items addObjectsFromArray:self.leftItems];
  343. // }
  344. //
  345. // [items addObject:self.flexibleSpaceItem];
  346. //
  347. // if (self.rightItems.count>0) {
  348. // [items addObjectsFromArray:self.rightItems];
  349. // }
  350. //
  351. // [self.toolBar setItems:items];
  352. }
  353. #pragma mark - Delegate Collectioin
  354. #pragma mark - UIPickerViewDataSource
  355. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  356. return 2;
  357. }
  358. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  359. if (component == 0) {
  360. return 1;
  361. }
  362. return maxCount;
  363. }
  364. #pragma mark - UIPickerViewDelegate
  365. - (nullable NSString *)pickerView:(UIPickerView *)pickerView
  366. titleForRow:(NSInteger)row forComponent:(NSInteger)component {
  367. if (component == 0) {
  368. NSInteger index = row % self.yearCount;
  369. NSString *text = [NSString stringWithFormat:@"%ld", self.minimumYear+index];
  370. if (self.isShowToday &&
  371. text.integerValue == self.thisYear &&
  372. self.selectedYear == self.thisYear &&
  373. self.selectedMonth == self.thisMonth) {
  374. text = @"至今";
  375. }
  376. return text;
  377. } else {
  378. NSInteger index = row % self.monthCount+1;
  379. if (self.isHideMonth &&
  380. index == self.thisMonth &&
  381. self.selectedMonth == self.thisMonth &&
  382. self.selectedYear == self.thisYear) {
  383. return @"--";
  384. }
  385. return [NSString stringWithFormat:@"%02ld", index];
  386. }
  387. }
  388. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  389. if (component == 0) {
  390. self.selectedYear = row%self.yearCount+self.minimumYear;
  391. if (row<maxCount/4 || row > maxCount/4*3) {
  392. [self setDefaultYear:self.selectedYear];
  393. }
  394. } else {
  395. self.selectedMonth = row%self.monthCount+1;
  396. if (row<maxCount/4 || row > maxCount/4*3) {
  397. [self setDefaultMonth:self.selectedMonth];
  398. }
  399. }
  400. if (self.selectedYear == self.maximumYear &&
  401. self.selectedMonth > self.maximumMonth) {
  402. self.defaultMonth = self.maximumMonth;
  403. }
  404. if (self.selectedYear == self.minimumYear &&
  405. self.selectedMonth < self.minimumMonth) {
  406. self.defaultMonth = self.minimumMonth;
  407. }
  408. if (self.isShowToday) {
  409. if ((self.selectedYear != self.thisYear &&
  410. self.selectedMonth == self.thisMonth) ||
  411. (self.selectedYear == self.thisYear &&
  412. self.selectedMonth != self.thisMonth)) {
  413. self.hideMonth = NO;
  414. [self.pickerView reloadAllComponents];
  415. }
  416. if ( self.selectedYear == self.thisYear &&
  417. self.selectedMonth == self.thisMonth) {
  418. self.hideMonth = YES;
  419. [self.pickerView reloadAllComponents];
  420. }
  421. }
  422. }
  423. #pragma mark - Custom Accessors
  424. #pragma mark -
  425. - (void)setDefaultYear:(NSInteger)defaultYear {
  426. if (defaultYear> self.maximumYear) {
  427. _defaultYear = self.maximumYear;
  428. } else if (defaultYear < self.minimumYear) {
  429. _defaultYear = self.minimumYear;
  430. } else {
  431. _defaultYear = defaultYear;
  432. }
  433. NSInteger selctRow = (maxCount/2)/self.yearCount*self.yearCount+ self.defaultYear- self.minimumYear;
  434. self.selectedYear = defaultYear;
  435. [self.pickerView reloadComponent:0];
  436. [self.pickerView selectRow:selctRow inComponent:0 animated:NO];
  437. }
  438. - (void)setDefaultMonth:(NSInteger)defaultMonth {
  439. if (defaultMonth> 12) {
  440. _defaultMonth = 12;
  441. } else if (defaultMonth < 1) {
  442. _defaultYear = 1;
  443. } else {
  444. _defaultMonth = defaultMonth;
  445. }
  446. _defaultMonth = defaultMonth;
  447. NSInteger selectRow = (maxCount/2)/self.monthCount*self.monthCount+ self.defaultMonth-1;
  448. self.selectedMonth = defaultMonth;
  449. [self.pickerView reloadComponent:1];
  450. [self.pickerView selectRow:selectRow inComponent:1 animated:NO];
  451. }
  452. - (void)setShowToday:(BOOL)showToday {
  453. _showToday = showToday;
  454. }
  455. - (void)setMinimumYear:(NSInteger)minimumYear {
  456. _minimumYear = minimumYear;
  457. self.yearCount = _maximumYear - _minimumYear+1;
  458. }
  459. - (void)setMaximumYear:(NSInteger)maximumYear {
  460. _maximumYear = maximumYear;
  461. self.yearCount = _maximumYear - _minimumYear+1;
  462. }
  463. - (void)setYearCount:(NSInteger)yearCount {
  464. _yearCount = yearCount;
  465. }
  466. - (NSInteger)yearCount {
  467. return _yearCount;
  468. }
  469. - (void)setMaximumDate:(NSDate *)maximumDate {
  470. if (maximumDate == nil) {
  471. maximumDate = [self.dateFormatter dateFromString:@"2018-12"];
  472. }
  473. _maximumDate = [maximumDate copy];
  474. kITLocalDate(_maximumDate);
  475. NSCalendar* calendar = [NSCalendar currentCalendar];
  476. NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth fromDate:_maximumDate];
  477. self.maximumYear = [components year];
  478. self.maximumMonth = [components month];
  479. }
  480. - (void)setMinimumDate:(NSDate *)minimumDate {
  481. if (minimumDate == nil) {
  482. minimumDate = [self.dateFormatter dateFromString:@"2018-01"];
  483. }
  484. _minimumDate = [minimumDate copy];
  485. kITLocalDate(_minimumDate);
  486. NSCalendar* calendar = [NSCalendar currentCalendar];
  487. NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth fromDate:_minimumDate];
  488. self.minimumYear = [components year];
  489. self.minimumMonth = [components month];
  490. }
  491. - (void)setDefaultDate:(NSDate *)defaultDate {
  492. if (defaultDate == nil) {
  493. defaultDate = [NSDate date];
  494. }
  495. _defaultDate = [defaultDate copy];
  496. NSCalendar* calendar = [NSCalendar currentCalendar];
  497. NSDateComponents* components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth fromDate:_defaultDate];
  498. NSInteger year = [components year];
  499. NSInteger month = [components month];
  500. [self setDefaultYear:year];
  501. [self setDefaultMonth:month];
  502. }
  503. - (void)setLeftItems:(NSArray *)leftItems {
  504. _leftItems = [leftItems copy];
  505. [self resetToolbar];
  506. }
  507. - (void)setRightItems:(NSArray *)rightItems {
  508. _rightItems = rightItems;
  509. [self resetToolbar];
  510. }
  511. - (NSDateFormatter *)dateFormatter {
  512. if (!_dateFormatter) {
  513. _dateFormatter = [[NSDateFormatter alloc] init];
  514. _dateFormatter.dateFormat = @"yyyy-MM";
  515. }
  516. return _dateFormatter;
  517. }
  518. - (UIBarButtonItem *)cancelButton {
  519. if (!_cancelButton) {
  520. _cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonOnClicked:)];
  521. }
  522. return _cancelButton;
  523. }
  524. - (UIBarButtonItem *)flexibleSpaceItem {
  525. if (!_flexibleSpaceItem) {
  526. _flexibleSpaceItem =
  527. [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
  528. target:self
  529. action:nil];
  530. }
  531. return _flexibleSpaceItem;
  532. }
  533. - (UIBarButtonItem *)confirmButton {
  534. if (!_confirmButton) {
  535. _confirmButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(confirmButtonOnClicked:)];
  536. }
  537. return _confirmButton;
  538. }
  539. - (UIView *)line {
  540. if (!_line) {
  541. _line = [[UIView alloc] initWithFrame:CGRectMake(0, 39.5, kITScreenWidth, 0.5)];
  542. _line.backgroundColor = [UIColor colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:1];
  543. }
  544. return _line;
  545. }
  546. - (UIView *)toolBar {
  547. if (!_toolBar) {
  548. _toolBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kITScreenWidth, 44)];
  549. // _toolBar.translucent = NO;
  550. _toolBar.backgroundColor = [UIColor homeRedColor];
  551. self.leftItems = @[self.cancelButton];
  552. self.rightItems = @[self.confirmButton];
  553. // [self resetToolbar];
  554. UIButton *left = [[UIButton alloc] initWithFrame:CGRectMake(5, 0, 44, 44)];
  555. [left setTitle:@"取消" forState:UIControlStateNormal];
  556. left.titleLabel.font = [UIFont systemFontOfSize:15];
  557. [left setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  558. [left addTarget:self action:@selector(cancelButtonOnClicked:) forControlEvents:UIControlEventTouchUpInside];
  559. [_toolBar addSubview:left];
  560. UIButton *right = [[UIButton alloc] initWithFrame:CGRectMake(kITScreenWidth-5-44, 0, 44, 44)];
  561. [right setTitle:@"确定" forState:UIControlStateNormal];
  562. right.titleLabel.font = [UIFont systemFontOfSize:15];
  563. [right setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  564. [right addTarget:self action:@selector(confirmButtonOnClicked:) forControlEvents:UIControlEventTouchUpInside];
  565. [_toolBar addSubview:right];
  566. UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
  567. title.font = [UIFont systemFontOfSize:15];
  568. title.textColor = [UIColor whiteColor];
  569. title.textAlignment = NSTextAlignmentCenter;
  570. title.text = @"选择月份";
  571. title.centerX = _toolBar.centerX;
  572. [_toolBar addSubview:title];
  573. // [_toolBar addSubview:self.line];
  574. }
  575. return _toolBar;
  576. }
  577. - (UIPickerView *)pickerView {
  578. if (!_pickerView) {
  579. _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, kITScreenWidth, 216)];
  580. _pickerView.backgroundColor = [UIColor whiteColor];
  581. _pickerView.delegate = self;
  582. _pickerView.dataSource = self;
  583. }
  584. return _pickerView;
  585. }
  586. @end