更改datePicker线条和字体颜色

问题描述 投票:2回答:2

任何人都知道如何更改UIDatePicker的行和字体颜色,并避免使用私有Apple api?我找到了这个,但它是非常脏的解决方案:

        datePicker.setValue(UIColor.white, forKey: "textColor")

        for subview in self.datePicker.subviews {

            if subview.frame.height <= 5 {

                subview.backgroundColor = UIColor.white
                subview.tintColor = UIColor.white
                subview.layer.borderColor = UIColor.white.cgColor
                subview.layer.borderWidth = 0.5            }
        }

        if let pickerView = self.datePicker.subviews.first {

            for subview in pickerView.subviews {

                if subview.frame.height <= 5 {

                    subview.backgroundColor = UIColor.white
                    subview.tintColor = UIColor.white
                    subview.layer.borderColor = UIColor.white.cgColor
                    subview.layer.borderWidth = 0.5
                }
            }
            self.datePicker.setValue(UIColor.white, forKey: "textColor")
        }
ios swift xcode customization uidatepicker
2个回答
0
投票

试试这个:

enter image description here

或按代码:

datePicker.setValue(UIColor.red, forKeyPath: "textColor")

0
投票

要更改DatePickerView所选矩形的颜色,您可以这样做:

datePicker.subviews[0].subviews[1].layer.borderColor = UIColor.red.cgColor //any color you want
datePicker.subviews[0].subviews[1].layer.borderWidth = 1
datePicker.subviews[0].subviews[2].layer.borderColor = UIColor.red.cgColor //any color you want
datePicker.subviews[0].subviews[2].layer.borderWidth = 1

subviews[1]是上边界,subviews[2]是下边界。 你也应该设置borderWidth使其可见

要在整个DatePicker中设置文本的颜色,您可以这样做:

datePicker.setValue(UIColor.red, forKey: "textColor")
© www.soinside.com 2019 - 2024. All rights reserved.