Modify UIDatePicker selection indicator tint color

2019-07-19 17:39发布

Is it possible to modify the tint color of a UIDatePicker's selection indicator? Or can it be removed so I can add my own selection indicator image?

I know in the case of UIPickerView´s you can disable the selection indicator so that you can provide your own selection indicator image.

2条回答
The star\"
2楼-- · 2019-07-19 18:18

Here is what I did with my UIDatePicker that was placed inside a UITableViewCell and sized purely using autolayout constraints. (I recommend subclassing the UIDatePicker).

let upperSelectorView = UIView()
let lowerSelectorView = UIView()

required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        upperSelectorView.backgroundColor = UIColor(colorLiteralRed: 127, green: 139, blue: 131, alpha: 0.45)
        self.addSubview(upperSelectorView)

        lowerSelectorView.backgroundColor = UIColor(colorLiteralRed: 127, green: 139, blue: 131, alpha: 0.45)
        self.addSubview(lowerSelectorView)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        print(self.frame.size.height)
        if (self.frame.size.height == 216) { //height it used in portrait.
            upperSelectorView.frame = CGRect(x: 0, y: 91, width: self.frame.size.width, height: 0.5)
            lowerSelectorView.frame = CGRect(x: 0, y: 125, width: self.frame.size.width, height: 0.5)
        }

        if (self.frame.size.height == 162) { //height it used in landscape.
            upperSelectorView.frame = CGRect(x: 0, y: 63, width: self.frame.size.width, height: 0.5)
            lowerSelectorView.frame = CGRect(x: 0, y: 98, width: self.frame.size.width, height: 0.5)
        }

    }

Tested to work on iPhone 6 and iPhone 6+.

查看更多
Animai°情兽
3楼-- · 2019-07-19 18:27

There is no option to change the color of selection indicator by default. You can make a workaround though. I would add a simple UIView with a color of your choice and alpha value for transparency. Se the size and coordinates accordingly to the original selection indicator to overlay it (size 280x44, coordinates 20/130 for standard picker - not sure about that so find out the right values in case these don't match).

UIView *overlayIndicator = [[UIView alloc] initWithFrame:CGRectMake(20, 130, 280, 44)];
overlayIndicator.backgroundColor = [UIColor redColor];
overlayIndicator.alpha = 0.5f;
[yourDatePicker addSubview:overlayIndicator];

Hope it's something you can start with.

enter image description here

查看更多
登录 后发表回答