iOS-日期选择控件(可单独选年/年月/年月日)

说明

iOS系统提供的UIDatePicker 只能用于选择年月日,通过借鉴stackoverflow上的思路对月日图层的隐藏来达到显示年/ 年月的效果

效果展示

通过分段控制器 选择年 弹出仅有年份选择的datePicker

通过分段控制器 选择月 弹出仅有年月选择的datePicker

通过分段控制器 选择日 弹出有年月日选择的datePicker

实现的主要代码

override func layoutSubviews() {
        super.layoutSubviews()
        guard datePickerView.subviews.first?.subviews.first?.subviews.count == 3 else {
            return
        }
        datePickerView.subviews.first?.subviews.first?.subviews[1].frame.origin.x = 0
        datePickerView.subviews.first?.subviews.first?.subviews[0].frame.origin.x = 0
        if type == .month {
            datePickerView.subviews.first?.subviews.first?.subviews[0].isHidden = false
            datePickerView.subviews.first?.subviews.first?.subviews[1].isHidden = false
            datePickerView.subviews.first?.subviews.first?.subviews[2].isHidden = true
            datePickerView.subviews.first?.subviews.first?.subviews[1].frame.origin.x += 100
        } else if type == .year {
            datePickerView.subviews.first?.subviews.first?.subviews[0].isHidden = false
            datePickerView.subviews.first?.subviews.first?.subviews[1].isHidden = true
            datePickerView.subviews.first?.subviews.first?.subviews[2].isHidden = true
            datePickerView.subviews.first?.subviews.first?.subviews[0].frame.origin.x += 70
        } else {
           datePickerView.subviews.first?.subviews.first?.subviews[0].isHidden = false
           datePickerView.subviews.first?.subviews.first?.subviews[1].isHidden = false
           datePickerView.subviews.first?.subviews.first?.subviews[2].isHidden = false
        }
    }

Demo下载地址 (github)

github地址:

写在最后 本人iOS开发菜鸟一枚, 不妥之处,还望大神指教~~~~~~~~~~~

Loading Disqus comments...
Table of Contents