http://stackoverflow.com/questions/6948297/uidatepicker-odd-behavior-when-setting-minuteinterval

Here's yet another approach, with an Objective-C category!

I took the spirit of @zurbergram's rounding behavior (up/down to closest) and @mmorris's overall answer and came up with this category:

#import <UIKit/UIKit.h>

@interface UIDatePicker (SetDateRounded)

-(void)setMinimumDateRoundedByMinuteInterval:(NSDate *)minimumDate;
-(void)setDateRoundedByMinuteInterval:(NSDate *)date animated:(BOOL)animatedYesNo; @end @implementation UIDatePicker (SetDateRounded) -(void)setDateRoundedByMinuteInterval:(NSDate *)date animated:(BOOL)animatedYesNo
{
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:date];
NSInteger minutes = [dateComponents minute];
NSInteger minutesRounded = roundf((float)minutes / (float)[self minuteInterval]) * self.minuteInterval;
NSDate *roundedDate = [[NSDate alloc] initWithTimeInterval:60.0 * (minutesRounded - minutes) sinceDate:date];
[self setDate:roundedDate animated:animatedYesNo];
} -(void)setMinimumDateRoundedByMinuteInterval:(NSDate *)date
{
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:date];
NSInteger minutes = [dateComponents minute];
NSInteger minutesRounded = roundf((float)minutes / (float)[self minuteInterval]) * self.minuteInterval;
NSDate *roundedDate = [[NSDate alloc] initWithTimeInterval:60.0 * (minutesRounded - minutes) sinceDate:date];
[self setMinimumDate:roundedDate];
} @end

Then in your implementation, you can do something like this:

#import "UIDatePicker+SetDateRounded.h"

...

- (void)viewDidLoad
{
[super viewDidLoad]; _datePicker.minuteInterval = 15; [_datePicker setMinimumDateRoundedByMinuteInterval:[NSDate date]];
[_datePicker setDateRoundedByMinuteInterval:[NSDate date] animated:YES];
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. TCP四种计时器
  2. 如何设置让基于matplotlib的绘图库正常的显示no-ascii字符(中文字符)
  3. SOA和WCF&amp;WebAPI
  4. 【转载】HTTP 错误 500.19 - Internal Server Error
  5. Android 项目实战--手机卫士(实现splash)
  6. mysql 乱码问题
  7. js模拟类
  8. AIX修改用户密码登录不成功案例分享
  9. ACM题目————已知前序和中序求后序
  10. OpenGL第15,16,17讲小结
  11. 2016 GDCPC 省赛总结
  12. quick-cocos2d-x游戏开发【5】——创建菜单
  13. 【卷一】正则四 |&gt; 练习
  14. PKI 笔记
  15. Linux之shell编程函数使用
  16. 蓝桥杯-循环节长度-java
  17. SVG 入门——理解viewport,viewbox,preserveAspectRatio
  18. 转发 JBPM工作流小结
  19. C# 文字滚动特效(上下滚动)
  20. vs2012升级vs2017后的一些坑

热门文章

  1. [20190727NOIP模拟测试9]单(single) 题解(树上dp)
  2. Laex/Delphi-OpenCV
  3. c++primer,自定义一个复数类
  4. spring aop思想
  5. 用原生js写小游戏--Flappy Bird
  6. BPT(Business Process Testing)
  7. http常见7种请求
  8. Dubbo 系列(07-5)集群容错 - Mock
  9. ==和equal的区别
  10. go 学习之gorm