我正在使用UIDatePicker控制器在iPhone應用程序中選擇日期。現在,我想顯示日曆視圖,而不是UIDatePicker控制器。我在谷歌搜索了我的最佳水平。但是,我找不到我的問題的確切解決方案。任何人都可以請幫助使用日曆視圖的UIDatePicker?提前致謝。如何顯示日曆而不是在iPhone中使用UIDatePicker?
3
A
回答
9
最好的方法是使用下面的一個 -
1)https://github.com/guicocoa/calendar
2)http://github.com/devinross/tapkulibrary
3)http://github.com/klazuka/Kal
沒有現成的解決方案,可轉換的UIDatePicker到日曆視圖
1
創建一個collectionview和用戶此單元格爲d isplay日曆。
calenderCell.h
#import <UIKit/UIKit.h>
//#import "UIView+Alignment.h"
//#import "UIView+Layout.h"
@protocol CalendarCellDelegte <NSObject>
- (void)updateSelectedDate:(NSDate*)date;
@end
@interface CalendarCell : UICollectionViewCell
{
NSMutableArray *labelArray;
NSDateComponents *currentMonth;
__weak id _delegate;
}
@property (nonatomic, weak) id delegate;
@property(nonatomic, retain) NSCalendar *calendar;
@property (atomic, strong) NSDate *firstDay;
- (void)UpdateCalendarCell:(NSDateComponents*)month_ selectedDate:(NSDate*)selDate;
@end
calenderCell.m
#import "CalendarCell.h"
@implementation CalendarCell
@synthesize calendar=_calendar;
@synthesize delegate=_delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CalendarCell" owner:self options:nil];
if ([arrayOfViews count] < 1) {
return nil;
}
if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
return nil;
}
self = [arrayOfViews objectAtIndex:0];
labelArray=[[NSMutableArray alloc]init];
for (int i=0; i<5; i++)
{
for (int j=0; j<7; j++)
{
CGRect rect;
rect=CGRectMake(((j*2)+2)+(j*44), (i*44), 42, 42);
// NSLog(@"%f",rect.origin.x);
UIView *view=[[UIView alloc]initWithFrame:rect];
[self addSubview:view];
view.layer.cornerRadius=view.bounds.size.width/2;
view.layer.borderColor=[UIColor clearColor].CGColor;
view.layer.borderWidth=1;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTap:)];
[view addGestureRecognizer:tapGestureRecognizer];
view.backgroundColor=[UIColor redColor];
rect=CGRectMake(6, 3, 30, 30);
UILabel *label=[[UILabel alloc]initWithFrame:rect];
label.backgroundColor=[UIColor greenColor];//UIColorFromRGB(0xd3d3d3);
[view addSubview:label];
label.textColor=[UIColor whiteColor];
label.textAlignment=NSTextAlignmentCenter;
label.userInteractionEnabled=NO;
[labelArray addObject:label];
label.layer.cornerRadius=15;
// label.backgroundColor=[UIColor clearColor];UIColorFromRGB(0x6cc9ff);
}
}
self.calendar=[NSCalendar currentCalendar];
}
return self;
}
- (void)UpdateCalendarCell:(NSDateComponents*)month_ selectedDate:(NSDate*)selDate
{
currentMonth=month_;
currentMonth.day=1;
[self setFirstDay:[currentMonth.calendar dateFromComponents:currentMonth]];
int viewTag=1;
NSInteger column = 7 - [self numberOfDaysInFirstWeek];
int totalNumberOfDays=(int)[self numberOfDays];
for (int i=0; i<5; i++)
{
for (int j=0; j<7; j++)
{
UILabel *label=(UILabel*)[labelArray objectAtIndex:viewTag-1];
label.textColor=[UIColor whiteColor];
label.backgroundColor=[UIColor clearColor];
label.superview.backgroundColor=[UIColor clearColor];
label.superview.layer.borderColor=[UIColor clearColor].CGColor;
if((viewTag-column) <= 0)
{
[email protected]"";
label.tag=0;
if(totalNumberOfDays+column > 35)
{
if(35- (column-viewTag) <= totalNumberOfDays)
{
label.text=[NSString stringWithFormat:@"%li",35- (column-viewTag)];
label.tag=35- (column-viewTag);
currentMonth.day=35- (column-viewTag);
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:currentMonth];
NSDateComponents *otherDay = [[NSCalendar currentCalendar] components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date];
if(selDate)
{
NSDateComponents *selectDate = [[NSCalendar currentCalendar] components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:selDate];
if([otherDay day] == [selectDate day] && [otherDay month] == [selectDate month] && [otherDay year] == [selectDate year] && [otherDay era] == [selectDate era])
{
label.backgroundColor=[UIColor clearColor];
label.superview.backgroundColor=[UIColor colorWithRed:23.0/255.0 green:154.0/255.0 blue:208.0/255.0 alpha:.5];//UIColorFromRGB(0x4DBDED);
label.superview.layer.borderColor=[UIColor clearColor].CGColor;
}
}
}
}
}
else if((viewTag-column) > [self numberOfDays])
{
label.text=[NSString stringWithFormat:@"%li",- ([self numberOfDays]- (viewTag-column))];
label.tag=0;
label.textColor=[UIColor lightGrayColor];
}
else
{
label.text=[NSString stringWithFormat:@"%li",viewTag-column];
label.tag=viewTag-column;
currentMonth.day=viewTag-column;
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:currentMonth];
NSDateComponents *otherDay = [[NSCalendar currentCalendar] components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date];
NSDateComponents *today = [[NSCalendar currentCalendar] components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
if([today day] == [otherDay day] && [today month] == [otherDay month] && [today year] == [otherDay year] && [today era] == [otherDay era])
{
label.backgroundColor=[UIColor clearColor];//23 154 208
label.superview.backgroundColor=[UIColor redColor];
label.superview.layer.borderColor=[UIColor clearColor].CGColor;
}
if(selDate)
{
NSDateComponents *selectDate = [[NSCalendar currentCalendar] components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:selDate];
if([otherDay day] == [selectDate day] && [otherDay month] == [selectDate month] && [otherDay year] == [selectDate year] && [otherDay era] == [selectDate era])
{
label.backgroundColor=[UIColor clearColor];
label.superview.backgroundColor=[UIColor colorWithRed:23.0/255.0 green:154.0/255.0 blue:208.0/255.0 alpha:.5];
label.superview.layer.borderColor=[UIColor clearColor].CGColor;
}
}
}
viewTag++;
}
}
}
- (void)actionTap:(UITapGestureRecognizer*)tap
{
@try {
UIView *view=(UIView*)tap.view;
UILabel *label=(UILabel*)[view.subviews objectAtIndex:0];
//if(currentMonth.day == label.tag)
// return;
if(label.tag != 0)
{
currentMonth.day=label.tag;
NSDate *selectedDate = [[NSCalendar currentCalendar] dateFromComponents:currentMonth];
[_delegate updateSelectedDate:selectedDate];
}
}
@catch (NSException *exception) {
NSLog(@"%@",exception);
}
}
#pragma mark - Helper methods
- (NSInteger)numberOfWeeks {
return [[self calendar] rangeOfUnit:NSDayCalendarUnit
inUnit:NSWeekCalendarUnit
forDate:[self firstDay]].length;
}
- (NSInteger)numberOfDays {
return [[self calendar] rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:[self firstDay]].length;
}
- (NSInteger)numberOfDaysInFirstWeek {
return [[self calendar] rangeOfUnit:NSDayCalendarUnit
inUnit:NSWeekCalendarUnit
forDate:[self firstDay]].length;
}
@end
相關問題
- 1. 而不是UIDatePicker的日曆視圖
- 2. 如何顯示日期爲feb-04-2012而不是02/04/2012在uidatepicker
- 3. 只需要在iPhone中顯示日期和日期UIDatePicker
- 4. 在日曆中顯示圖像而不是文字「NextPrev」
- 5. 在iPhone的UIDatePickerModeDate中顯示UIDatePicker中的特定日期
- 6. iphone UIDatePicker在中央時區顯示錯誤的日期
- 7. 非公曆日曆的UIDatePicker
- 8. 如何使用動畫顯示日曆
- 9. 如何在日期中顯示timeEditor使用rich:日曆
- 10. 如何顯示日曆nextweek日曆
- 11. 如何在UIDatePicker中僅顯示時間iphone
- 12. UIDatePicker只顯示月和日
- 13. 如何在Drupal中顯示日曆?
- 14. 如何在java中顯示日曆
- 15. 從iPhone中的UIDatePicker顯示星期幾
- 16. 在日曆中顯示內容/節點日期而不是文本
- 17. 使用jquery顯示日曆
- 18. 日曆顯示不正確使用flatpickr
- 19. 日曆不顯示使用sfJQueryUIPlugin
- 20. 如何在iPad中顯示UIDatePicker?
- 21. 我可以使用DatePicker而不顯示日曆嗎?
- 22. 如何在UIDatePicker中僅禁用日期選擇而不是時間選擇
- 23. 角引導日曆不顯示日曆
- 24. toInstant()在日曆中GMT是展示,而不是當地時間
- 25. 如何在UIDatePicker中顯示5周的日期和時間?
- 26. 如何在matplotlib x軸中顯示日期而不是序號
- 27. 使用JavaScript顯示農曆日曆
- 28. 如何在iphone sdk中將日曆存儲在日曆中?
- 29. 如何顯示日曆事件的主題而不削減它 - 使用FullCalendar UI
- 30. 如何在日曆的左側顯示即將發生的事件在日曆中顯示日曆?
@ Yuvaraj.M我個人使用卡爾從Klazuka,我會推薦它。但是它有一個問題:如果你想在選擇一個日期後打開一個新的視圖。所以如果你選擇上一個/下一個月,它會自動選擇那個月的第一天。我嘗試了tapku,但日曆非常混亂。本週的日期和日期不正確。也許它現在已經修復了,幾個月前我檢查過了。 – Novarg
@Novarg謝謝。我會檢查並儘快回覆。謝謝。 –