欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UIDatePicker 只显示年和月滚轮

程序员文章站 2022-06-07 12:28:31
...

今天在做项目的时候有一块设计的是选择日期,但是只精确到月.
但是OC中UIDatePicker的mode选项里并没有精确到月的选项,所以就要自定义一个UIDatePicker.
当时想到的解决方案有两种:
第一种是直接用pickerview来代替UIDatePicker.自定义年限和月份.
第二种是利用view的layer.masksToBounds = YES 属性来操作.创建两个UIDatePicker 一个只显示年,一个只显示月.
我这里显示的UIDatePicker的大小是cgsize(300,100)
其中年份的显示在0-120,月份的显示是在115-195.
还有就是滚轮中间的两条线 在高度是100的情况下 第一条线的y坐标是32 第二条线y坐标是67 高度都为1 颜色为[UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1];
大家可以根据需要按照比例进行计算 显示的范围和线的高度

        UIView *pickerBGView = [[UIView alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(applyTimeLabel.frame), 120, 100)];
        pickerBGView.layer.masksToBounds = YES;
        [self.whiteBGView addSubview:pickerBGView];
        
        
        self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];
        self.datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
        self.datePicker.datePickerMode = UIDatePickerModeDate;

        //监听DataPicker的滚动
        [self.datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
        [pickerBGView addSubview:self.datePicker];
        
        
        UIView *pickerMonthBGView = [[UIView alloc] initWithFrame:CGRectMake(self.whiteBGView.frame.size.width-100, CGRectGetMaxY(applyTimeLabel.frame), 80, 100)];
        pickerMonthBGView.layer.masksToBounds = YES;
        [self.whiteBGView addSubview:pickerMonthBGView];
        
        
        self.monthDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(-115, 0, 300, 100)];
        self.monthDatePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
        self.monthDatePicker.datePickerMode = UIDatePickerModeDate;
        //监听DataPicker的滚动
        [self.monthDatePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
        [pickerMonthBGView addSubview:self.monthDatePicker];
        
        self.lineView1 = [[UIView alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(applyTimeLabel.frame)+32, self.whiteBGView.frame.size.width-32, 1)];
        self.lineView1.backgroundColor = LineColor;
        [self.whiteBGView addSubview:self.lineView1];
        
        self.lineView2 = [[UIView alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(applyTimeLabel.frame)+67, self.whiteBGView.frame.size.width-32, 1)];
        self.lineView2.backgroundColor = LineColor;
        [self.whiteBGView addSubview:self.lineView2];
- (void)dateChange:(UIDatePicker *)datePicker {
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    //设置时间格式
    formatter.dateFormat = @"yyyy";
    NSDateFormatter *formatterMonth = [[NSDateFormatter alloc] init];
    
    //设置时间格式
    formatterMonth.dateFormat = @"MM";
    self.dateStr = [NSString stringWithFormat:@"%@%@",[formatter  stringFromDate:self.datePicker.date],[formatterMonth  stringFromDate:self.monthDatePicker.date]];
    NSLog(@"%@",self.dateStr);
}