Swift — UIPickerView实现生日选择器

2020-12-21  本文已影响0人  土豆骑士

实现自定义的生日选择器,需要修改文字颜色,字体,背景。
使用系统UIPickerView实现。
PS:在iOS14以下展示的选中样式与iOS14以下的样式不同。
GitHub地址

iOS 14 以下UIPickerView
IMG_0524.jpg
iOS 14 以上UIPickerView
IMG_0808.jpg

部分代码:

extension Social.DatePickerView: UIPickerViewDelegate, UIPickerViewDataSource {
    
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return componentWidth
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let label : UILabel
        if view is UILabel {
            label = view as! UILabel
        } else {
            label = rowLabel
        }
        for v in pickerView.subviews where v.frame.size.height < 1 {
            v.backgroundColor = .white
            v.alpha = 0.16
        }
        label.font = yearFont
        label.textColor = .white
        label.text = titleForRow(row, component: component)
        return label
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return rowHeight
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return componentsCount
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == DatePickerComponent.month.rawValue {
            return bigRowMonthCount
        } else if component == DatePickerComponent.day.rawValue {
            return bigRowDayCount
        }
        return bigRowYearCount
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        
        let month = (selectedRow(inComponent: DatePickerComponent.month.rawValue) + 1) % 12
        let day = selectedRow(inComponent: DatePickerComponent.day.rawValue) % 31
        let year = years[selectedRow(inComponent: DatePickerComponent.year.rawValue) % years.count].int ?? 2020
        
        if year % 4 == 0 {
            if month == 2 {
                if day == 29 || day == 30 {
                    let gap = day - 28
                    let daySelectedRow = selectedRow(inComponent: DatePickerComponent.day.rawValue)
                    selectRow(daySelectedRow - gap, inComponent: DatePickerComponent.day.rawValue, animated: true)
                }
            }
        } else {
            if month == 2 {
                if day == 28 || day == 29 || day == 30 {
                    let gap = day - 27
                    let daySelectedRow = selectedRow(inComponent: DatePickerComponent.day.rawValue)
                    selectRow(daySelectedRow - gap, inComponent: DatePickerComponent.day.rawValue, animated: true)
                }
            }
        }
    }
    
    private func titleForRow(_ row : Int, component : Int) -> String {
        if component == DatePickerComponent.month.rawValue {
            return months[row % months.count]
        } else if component == DatePickerComponent.day.rawValue {
            return days[row % days.count]
        }
        return years[row % years.count]
    }
}
上一篇下一篇

猜你喜欢

热点阅读