如何在 iPhone 中显示日历而不是使用 UIDatePicker?

发布于 2024-12-27 22:40:21 字数 155 浏览 3 评论 0原文

我正在使用 UIDatePicker 控制器在 iPhone 应用程序中选择日期。现在,我想显示日历视图而不是 UIDatePicker 控制器。我已经在谷歌上搜索到了我的最佳水平。但是,我找不到解决我的问题的确切解决方案。任何人都可以帮助使用 UIDatePicker 的日历视图吗?提前致谢。

I am using UIDatePicker controller to choose the date in iPhone app. Now, i want to show the Calendar View instead of UIDatePicker Controller. I have searched my level best in google. But, i can't find the exact solution for my problem. Can anyone please help to use the Calendar view for UIDatePicker? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卸妝后依然美 2025-01-03 22:40:21

最好的选择是使用以下之一 -

1) https://github.com/guicocoa/calendar

2) http://github.com/devinross/tapkulibrary

3) http://github.com/klazuka/Kal

没有现成的解决方案可以将 UIDatePicker 转换为日历视图

Your best bet is to use one of the following -

1) https://github.com/guicocoa/calendar

2) http://github.com/devinross/tapkulibrary

3) http://github.com/klazuka/Kal

There is no ready made solution which will convert UIDatePicker to calender view

心房敞 2025-01-03 22:40:21

创建一个集合视图并使用此单元格来显示日历。

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)
            {
                label.text=@"";
                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

Create a collectionview and user this cell to display calender.

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)
            {
                label.text=@"";
                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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文